ember数据数组UI不会在pushObject上更新 [英] ember data array UI not updating on pushObject

查看:102
本文介绍了ember数据数组UI不会在pushObject上更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我为我的模型获取的产品标签的列表。



路线:

  model:function(){
return {
product_tags:this.store.find('product-tag',{merchant:merchId})
}
}
pre>

我有一个组件可以在模型中添加标签,但是当我创建记录并将其推入模型(如其他帖子中的建议)时,我的UI仍然没有更新。

  addTag:function(name){
tag = this.store.createRecord('product-tag',{
name:name
});
this.model.product_tags.toArray()。addObject(tag);

tag.save();
}


// model merchant.js
导出默认DS.Model.extend({
user_id:DS.attr('number') ,
product_tags:DS.hasMany('product-tag',{async:true})
});

// model product-tag.js
export default DS.Model.extend({
merchant:DS.belongsTo('merchant'),
name: DS.attr('string'),
});

我做错了什么?提前致谢。

解决方案

你应该使它在路由中的数组,所以你可以随时使用它,就像你想要的。您的调用toArray(),它创建一个新的Array实例,那么你的模型不会挂钩到刚刚制作的数组。

  :function(){
return {
product_tags:this.store.query('product-tag',{merchant:merchId}))然后(function(pt){
return pt。 toArray();
});
}
}

var x = this.get('model.product_tags')=== model的p_t // true
var y = this.get( 'model.product_tags')。toArray()=== model的p_t // false

do

  addTag:function(name){
this.get('store')。createRecord('product-标签',{
name:name
})。save()。then(function(saved){
this.get('model.product_tags')。pushObject(saved);
} .bind(this);
}


I have a list of product-tag that I fetch for my model.

Route:

model: function() {
  return {
    product_tags: this.store.find('product-tag', {merchant: merchId})
  }
}

I have a component that adds tags to the model, however when after I create the record and push it into the model (as suggested on other posts) my UI still isn't updating.

addTag: function(name) {
  tag = this.store.createRecord('product-tag', {
    name: name
  });
  this.model.product_tags.toArray().addObject(tag);  

  tag.save();
}


//model merchant.js
export default DS.Model.extend({
  user_id: DS.attr('number'),
  product_tags: DS.hasMany('product-tag', {async: true})
});

//model product-tag.js
export default DS.Model.extend({
 merchant: DS.belongsTo('merchant'),
 name: DS.attr('string'),
});

What am I doing wrong? Thanks in advance.

解决方案

You should make it array in the route, so u can use it always afterwards like u want. Your calling toArray() which makes a new Array instance, then your model is not hooked to the array u just made.

  model: function() {
      return {
        product_tags: this.store.query('product-tag', {merchant: merchId}).then(function(pt) {
           return pt.toArray(); 
        });
      }
    }

   var x = this.get('model.product_tags') === model's p_t // true
   var y = this.get('model.product_tags').toArray() === model's p_t // false

Later on just do

addTag: function(name) {
  this.get('store').createRecord('product-tag', {
    name: name
  }).save().then(function(saved){ 
     this.get('model.product_tags').pushObject(saved);
  }.bind(this);  
}

这篇关于ember数据数组UI不会在pushObject上更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆