Ember.js - 无法获取或设置模型 属性
Ember.js - can't get or set model property
我有一个 Ember.js 模型,其固定装置如下所示:
App.Category = DS.Model.extend({
category: attr('string'),
friendly: attr('string'),
iconUrl: attr('string'),
isPrimary: attr('bool'),
isSecondary: attr('bool'),
isTertiaryOne: attr('bool'),
isTertiaryTwo: attr('bool')
});
App.Category.reopenClass({
FIXTURES: [
{
id: 1,
category: 'recommended',
friendly: 'recommended for you',
iconUrl: 'image1.png',
isPrimary: true,
isSecondary: false,
isTertiaryOne: false,
isTertiaryTwo: false
},
{
id: 2,
category: 'recent',
friendly: 'recently viewed',
iconUrl: 'image2.png',
isPrimary: false,
isSecondary: true,
isTertiaryOne: false,
isTertiaryTwo: false
}
]
});
我想要做的就是从特定模型中检索 属性 值,并在我的控制器中的操作中将其设置为新值:
App.CategoryController = Ember.ArrayController.extend({
actions: {
tileClick: function (selectedCategory) {
var cat = this.store.find('category', { category: selectedCategory });
console.log(cat.get('isPrimary'));
cat.set('isPrimary', true);
}
}
});
Emberjs 网站指南说我需要做的就是设置一个值:
var tyrion = this.store.find('person', 1);
// ...after the record has loaded
tyrion.set('firstName', "Yollo");
但这就是行不通。
变量 'cat' 存在,如果我足够深入地深入控制台中的对象,我可以看到我想要的属性,所以我知道正在选择正确的模型。
store.find
方法 returns 你是一个承诺,所以你必须(如你所写)等待它加载。
您应该多读一些关于 promises 的内容,但您现在可以做的是:
var cat = this.store.find('category', {
category: selectedCategory
}).then(function(categories) {
categories.forEach(function(category) {
category.set('isPrimary', true);
});
});
请注意,如果您使用查询参数(find
与一个对象有效 findQuery
),您将获得模型列表,而不是特定模型,即使只找到一个.
我有一个 Ember.js 模型,其固定装置如下所示:
App.Category = DS.Model.extend({
category: attr('string'),
friendly: attr('string'),
iconUrl: attr('string'),
isPrimary: attr('bool'),
isSecondary: attr('bool'),
isTertiaryOne: attr('bool'),
isTertiaryTwo: attr('bool')
});
App.Category.reopenClass({
FIXTURES: [
{
id: 1,
category: 'recommended',
friendly: 'recommended for you',
iconUrl: 'image1.png',
isPrimary: true,
isSecondary: false,
isTertiaryOne: false,
isTertiaryTwo: false
},
{
id: 2,
category: 'recent',
friendly: 'recently viewed',
iconUrl: 'image2.png',
isPrimary: false,
isSecondary: true,
isTertiaryOne: false,
isTertiaryTwo: false
}
]
});
我想要做的就是从特定模型中检索 属性 值,并在我的控制器中的操作中将其设置为新值:
App.CategoryController = Ember.ArrayController.extend({
actions: {
tileClick: function (selectedCategory) {
var cat = this.store.find('category', { category: selectedCategory });
console.log(cat.get('isPrimary'));
cat.set('isPrimary', true);
}
}
});
Emberjs 网站指南说我需要做的就是设置一个值:
var tyrion = this.store.find('person', 1);
// ...after the record has loaded
tyrion.set('firstName', "Yollo");
但这就是行不通。
变量 'cat' 存在,如果我足够深入地深入控制台中的对象,我可以看到我想要的属性,所以我知道正在选择正确的模型。
store.find
方法 returns 你是一个承诺,所以你必须(如你所写)等待它加载。
您应该多读一些关于 promises 的内容,但您现在可以做的是:
var cat = this.store.find('category', {
category: selectedCategory
}).then(function(categories) {
categories.forEach(function(category) {
category.set('isPrimary', true);
});
});
请注意,如果您使用查询参数(find
与一个对象有效 findQuery
),您将获得模型列表,而不是特定模型,即使只找到一个.