Ember.js - 无法获取或设置模型属性

时间:2021-05-15 21:06:54

I have an Ember.js model with fixtures that looks like this:

我有一个带有固定装置的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
        }
    ]
});

All I want to do is retrieve a property value from a specific model, and set it to a new value in an action in my controller:

我想要做的就是从特定模型中检索属性值,并将其设置为我的控制器中的操作中的新值:

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);
        }
    }
});

The Emberjs website guides say that all I have to do to set a value is :

Emberjs网站指南说我要做的就是设置一个值:

var tyrion = this.store.find('person', 1);
// ...after the record has loaded
tyrion.set('firstName', "Yollo");

but it just isn't working.

但它只是不起作用。

The variable 'cat' exists, and if I drill down deep enough into the object in the console, I can see the properties I want, so I know the correct model is being selected.

变量'cat'存在,如果我深入钻入控制台中的对象,我可以看到我想要的属性,所以我知道正在选择正确的模型。

1 个解决方案

#1


1  

The store.find method returns you a promise, so you have to (as you wrote) wait till it's loaded.

store.find方法返回一个promise,所以你必须(如你所写)等到它被加载。

You should read about promises more, but what you could do now is:

你应该更多地阅读承诺,但你现在可以做的是:

var cat = this.store.find('category', {
  category: selectedCategory
}).then(function(categories) {
  categories.forEach(function(category) {
    category.set('isPrimary', true);
  });
});

Note that if you're using query param (find with an object is effectively findQuery), you get the list of models, not a specific model, even if only one was found.

请注意,如果您正在使用查询参数(使用对象查找实际上是findQuery),您将获得模型列表,而不是特定模型,即使只找到一个。

#1


1  

The store.find method returns you a promise, so you have to (as you wrote) wait till it's loaded.

store.find方法返回一个promise,所以你必须(如你所写)等到它被加载。

You should read about promises more, but what you could do now is:

你应该更多地阅读承诺,但你现在可以做的是:

var cat = this.store.find('category', {
  category: selectedCategory
}).then(function(categories) {
  categories.forEach(function(category) {
    category.set('isPrimary', true);
  });
});

Note that if you're using query param (find with an object is effectively findQuery), you get the list of models, not a specific model, even if only one was found.

请注意,如果您正在使用查询参数(使用对象查找实际上是findQuery),您将获得模型列表,而不是特定模型,即使只找到一个。