本文整理匯總了TypeScript中@glimmer/object-reference.setProperty函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript setProperty函數的具體用法?TypeScript setProperty怎麽用?TypeScript setProperty使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了setProperty函數的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: set
export function set(obj, key, value) {
if (key.indexOf('.') !== -1) {
let path = key.split('.');
let parent = path.slice(0, -1).reduce((obj, key) => obj[key], obj);
setProperty(parent, path[path.length - 1], value);
} else {
setProperty(obj, key, value);
}
}
示例2: function
QUnit.test("basic reference data flow", function() {
let obj1 = { label: "obj1", model: { person: { name: { first: "Yehuda", last: "Katz" } } } };
let obj2 = { label: "obj2", model: { person: { name: obj1.model.person.name } } };
let obj3 = { label: "obj3", model: { person: obj1.model.person } };
let obj4 = { label: "obj4", model: obj1.model };
let originalPerson = obj1.model.person;
let o1 = [
addObserver(obj1, 'obj1', 'model.person.name.first'),
addObserver(obj1.model, 'obj1.model', 'person.name.first'),
addObserver(obj1.model.person, 'obj1.model.person', 'name.first'),
addObserver(obj1.model.person.name, 'obj1.model.person.name', 'first')
];
let o2 = [
addObserver(obj2, 'obj2', 'model.person.name.first'),
addObserver(obj2.model, 'obj2.model', 'person.name.first'),
addObserver(obj2.model.person, 'obj2.model.person', 'name.first'),
addObserver(obj2.model.person.name, 'obj2.model.person.name', 'first')
];
let o3 = [
addObserver(obj3, 'obj3', 'model.person.name.first'),
addObserver(obj3.model, 'obj3.model', 'person.name.first'),
addObserver(obj3.model.person, 'obj3.model.person', 'name.first'),
addObserver(obj3.model.person.name, 'obj3.model.person.name', 'first')
];
let o4 = [
addObserver(obj4, 'obj4', 'model.person.name.first'),
addObserver(obj4.model, 'obj4.model', 'person.name.first'),
addObserver(obj4.model.person, 'obj4.model.person', 'name.first'),
addObserver(obj4.model.person.name, 'obj4.model.person.name', 'first')
];
allDirty(o1, "Yehuda");
allDirty(o2, "Yehuda");
allDirty(o3, "Yehuda");
allDirty(o4, "Yehuda");
allClean(o1);
allClean(o2);
allClean(o3);
allClean(o4);
setProperty(obj1.model, 'person', { name: { first: 'Godfrey', last: 'Chan' } });
isDirty(o1[0], "Godfrey");
isDirty(o1[1], "Godfrey");
isClean(o1[2]);
isClean(o1[3]);
allClean(o2);
allClean(o3);
isDirty(o4[0], "Godfrey");
isDirty(o4[1], "Godfrey");
isClean(o4[2]);
isClean(o4[3]);
setProperty(originalPerson.name, 'first', "Godhuda");
isClean(o1[0]);
isClean(o1[1]);
isDirty(o1[2], "Godhuda");
isDirty(o1[3], "Godhuda");
allDirty(o2, "Godhuda");
allDirty(o3, "Godhuda");
isClean(o4[0]);
isClean(o4[1]);
isDirty(o4[2], "Godhuda");
isDirty(o4[3], "Godhuda");
setProperty(obj1.model, 'person', undefined);
isDirty(o1[0], undefined);
isDirty(o1[1], undefined);
isClean(o1[2]);
isClean(o1[3]);
allClean(o2);
allClean(o3);
isDirty(o4[0], undefined);
isDirty(o4[1], undefined);
isClean(o4[2]);
isClean(o4[3]);
setProperty(obj1.model, 'person', originalPerson);
isDirty(o1[0], "Godhuda");
isDirty(o1[1], "Godhuda");
isClean(o1[2]);
isClean(o1[3]);
allClean(o2);
allClean(o3);
//.........這裏部分代碼省略.........
示例3: equalsElement
@test "Inside a loop"() {
this.testType = 'Dynamic';
this.registerComponent("Basic", "FooBar", "<p>{{@value}}</p>");
let roots = [
{ id: 0, element: document.createElement('div'), value: 'foo' },
{ id: 1, element: document.createElement('div'), value: 'bar' },
{ id: 2, element: document.createElement('div'), value: 'baz' },
];
this.render(
stripTight`
{{~#each roots key="id" as |root|~}}
{{~#in-element root.element ~}}
{{component 'FooBar' value=root.value}}
{{~/in-element~}}
{{~/each}}
`,
{
roots
}
);
equalsElement(roots[0].element, 'div', {}, '<p>foo</p>');
equalsElement(roots[1].element, 'div', {}, '<p>bar</p>');
equalsElement(roots[2].element, 'div', {}, '<p>baz</p>');
this.assertHTML("<!----><!----><!--->");
this.assertStableRerender();
set(roots[0], "value", "qux!");
this.rerender();
equalsElement(roots[0].element, 'div', {}, '<p>qux!</p>');
equalsElement(roots[1].element, 'div', {}, '<p>bar</p>');
equalsElement(roots[2].element, 'div', {}, '<p>baz</p>');
this.assertHTML("<!----><!----><!--->");
this.assertStableRerender();
set(roots[1], "value", "derp");
this.rerender();
equalsElement(roots[0].element, 'div', {}, '<p>qux!</p>');
equalsElement(roots[1].element, 'div', {}, '<p>derp</p>');
equalsElement(roots[2].element, 'div', {}, '<p>baz</p>');
this.assertHTML("<!----><!----><!--->");
this.assertStableRerender();
set(roots[0], "value", "foo");
set(roots[1], "value", "bar");
this.rerender();
equalsElement(roots[0].element, 'div', {}, '<p>foo</p>');
equalsElement(roots[1].element, 'div', {}, '<p>bar</p>');
equalsElement(roots[2].element, 'div', {}, '<p>baz</p>');
this.assertHTML("<!----><!----><!--->");
this.assertStableRerender();
this.testType = 'Basic';
}