【问题标题】:Checking for model update in Computed Property检查计算属性中的模型更新
【发布时间】:2015-09-15 06:52:50
【问题描述】:

我正在创建一个用于呈现表格的组件。组件实际上是一组嵌套组件,在顶层接收路由模型和配置对象,然后在组件内部进行处理,并在下一个等中传递/迭代。

最终的子组件接收一个模型(仅代表表中的一行)以及定义从模型中显示哪个字段的字段名称。

所有这些都可以完美运行,并且 UI 更新绑定到模型。我遇到的问题是模型更新没有被推送到 UI。在我的子组件中,我使用以下内容绑定到 UI 元素:

tdVal : function(){

    return this.get('data').get(this.get('details').get('field'));

}.property()

tdValUpdated : function(){

    this.get('data').set(this.get('details').get('field'),this.get('val'));

}.property('tdVal'),

正如您所见,没有为 tdVal 设置计算属性字面量,这就是模型更新未推送到 UI 的原因。如果我要给它一个字面值,例如“data.status”,那么模型的状态更新会被推送到 UI。

我可以使用什么文字值来计算模型中的任何属性变化?

我已经尝试过 'data.isUpdated'、'data.isSaving' 等。我不能使用 'data.[]' 作为单个模型,而不是模型数组。

【问题讨论】:

  • 如果事先知道这些字段,您可以将它们列出来。
  • 好点,但它是一个高度通用的组件。有什么方法可以将 var 设置为正确的字段名称作为属性的键?我试过了,但我遇到了范围问题。

标签: ember.js ember-data


【解决方案1】:

好的,经过反复试验,我想我已经找到了解决方法。它很乱,我对此不太满意:

//as previous I render the the appropriate value from the model as defined 
//by the passed in config object
tdVal : function(){

    return this.get('data').get(this.get('details').get('field'));

}.property(),


//then detect UI changes and push to model if required
tdValUpdated : function(){

    this.get('data').set(this.get('details').get('field'),this.get('val'));

}.property('tdVal'),


//Then I observe any changes to model isSaving and directly set tdVal with
//the value of the field for the current td
generalUpdateCatch: function() {

    this.set('tdVal',this.get('data').get(this.get('details').get('field')));

}.observes('data.isSaving'),

我确实尝试了以下方法:

tdVal : function(){

    return this.get('data').get(this.get('details').get('field'));

}.observes('data.isSaving'),

但是得到错误:'Uncaught TypeError: unsupported content',不知道为什么?如果有人有更好的解决方案,请发布,因为我非常不喜欢这些解决方法。

【讨论】:

    猜你喜欢
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 2018-02-22
    • 2019-04-18
    • 1970-01-01
    相关资源
    最近更新 更多