gpt4 book ai didi

javascript - Ember.js 按名称查找模型项,然后将其添加到现有的不同模型 hasMany 记录中

转载 作者:行者123 更新时间:2023-11-28 08:41:55 24 4
gpt4 key购买 nike

伙计们,我无法理解它。现在挣扎了几个小时。我有一个可以有多个类别的文章模型:

// parent
App.Article = DS.Model.extend({
name: DS.attr('string'),
category: DS.hasMany('category')
)};

// child
App.Category = DS.Model.extend({
name: DS.attr('string')
});

通过单击自动完成组件结果,将执行以下操作:

  addCategory: function (categoryName) {
// for the sake of simplicity a default name
categoryName = 'car';

// find category instance by name
this.store.find('category').then(function(category) {
var categoryItem = category.filterBy('name', categoryName);

// get categories and add the category to it
this.get('category').pushObject(categoryItem);
});

},

您应该思考的问题是什么。嗯,问题是没有错误,一切看起来都很好。

但是:

  • 当我查看 Ember Inspector 时,我清楚地看到创建了一条新的文章记录,并且看到了已经创建的类别。但没有类别添加到 hasMany Category 记录中。

那么问题是,如何先查找一个Category项,然后将其添加到一篇Article的hasMany记录中?

大编辑:我删除了一些不相关的代码和文本。希望我的问题现在更清楚了。

最佳答案

呼呼!我可以回答我自己的问题。我很困惑如何使用 .then 函数的 this

我还稍微清理了我的代码:

addCategory: function (categoryName) {
var self = this;

// find category model by name
this.store.find('category').then(function(category) {
self.get('category').pushObject(category.findBy('name', categoryName));
});
}

非常感谢您的光临。

关于javascript - Ember.js 按名称查找模型项,然后将其添加到现有的不同模型 hasMany 记录中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20375696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com