2016-03-08 38 views
2

我使用this.store.push推记录进店从应用控制器(这个动作被从在应用控制器初始化一个Socket服务调用)更新模板,使用烬2.2.1我实现这个像Push记录到本地存储和

var newStoreRecord = this.store.push({ 
    data: { 
     id: id, 
     type: 'cart', 
     attributes: newCartItem 
    } 
}); 

这增加了这个新项目进店,但模板没有更新以显示新的项目,我也尝试添加像这样

this.get('cart.model').pushObject(newStoreRecord);假设我有一些东西如控制器顶部的cart: Ember.inject.controller(),,可能无论如何,都有这个错误。

在车路线我有我的模式被定义为这样

model(params) { 
    this.set('routeParams',params.event_url); 
    return Ember.RSVP.hash({ 
     event: null, 
     items: null 
    }); 
}, 
actions: { 
    didTransition() { 
    this.store.findRecord('event',this.get('routeParams')).then((result)=>{ 
     this.controller.set('model.event',result); 
    }); 
    this.controller.set('noItems',false); 
    this.store.query('cart',{auction_id:this.get('routeParams'),user:this.get('user.user.user_id'),combine:true}).then((result)=>{ 
     if(!result.get('length')){ 
     this.controller.set('noItems',true); 
     return null; 
     }else{ 
     this.controller.set('model.items',result); 
    } 
    }); 
    }, 
} 

不知道如果我在与获得的模板更新,因为我没有使用模型挂钩的烦恼? (顺便说一句,我们没有使用模型挂钩,因为Android上的糟糕表现,我们宁愿加载一个空模板与装载机,然后加载数据,而不是周围的其他方式

+0

您可以将您的模板代码?另外,你在哪里做'push()'?我也没有看到。 – mattmcmanus

回答

1

我这里有几个想法:

具体回答你的问题,当你从商店设置一个变量,像你这样做,只会参考什么是在商店当时,它不会自动更新。

最好的办法是两个新的计算属性添加到您的控制器:

items: Ember.computed(function() { 
    return this.store.peekAll('cart'); 
}), 

// You'll need to flesh this one out further 
filteredItems: Ember.computed('[email protected]_id', function() { 
    return this.get('items').filter(...); 
}) 

参考filteredItems在你的模板,它应该工作。

旁注,我会强烈建议重构有两件事情。

  • 我会用setupController挂钩,而不是didTransition。它运行模式钩齐全所以你正在寻找
  • 您可以access the params在路线的任何时间将类似于后,这样你就不会需要将其储存在模型中勾
  • 你不如果你没有做任何异步数据,就不需要在模型钩子中返回一个承诺。只需返回对象。你甚至需要这样做。

希望这会有所帮助。

+0

那么我想在你的代码中做什么?计算出的filteredItems是否只是在侦听某些东西发生变化,以便知道再次获取项目?如果是这样,我应该能够听到我知道的任何事情会改变,当我想再次正确加载项目? – Jordan

+0

对。 “PeekAll”是对商店的实时查询。所以无论何时您推送到商店,它都会更新,触发'filteredItems'计算属性来重新评估哪个会更新您的模板。 – mattmcmanus