强制控制器始终充当Ember中的模型的代理 [英] Force a controller to always act as a proxy to a model in Ember

查看:138
本文介绍了强制控制器始终充当Ember中的模型的代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在循环一个ArrayController内容被设置为一个RecordArray的内容。每个记录是DS.Model,说客户端

  {{#控制器中的每个项目}} 
{{item。余额}}
{{/ each}}

余额是客户端模型的属性并且对item.balance的调用将直接从模型中获取该属性。我想应用一些格式化以平衡以显示货币格式。这样做的简单方法是将一个计算属性balanceMoney添加到Client对象,然后执行格式:

 应用程序.Client = DS.Model({
balance:DS.attr('balance'),

balanceMoney:function(){
//格式化余额
return Money.format(this.get('balance');
} .property('balance')
});

这很好的目的,平衡Money计算属性的正确的地方是客户端控制器,而不是客户端模型,我的印象是控制器中的Ember查找属性首先,然后尝试在模型中检索它们,如果没有找到任何东西,这些都不会发生在这里,而对于item.balanceMoney的调用将被忽略,永远不会到达控制器。



可以在某种程度上配置控制器,在任何情况下始终作为模型的代理。



更新 - 使用lat从emberjs主数据库的最终版本可以通过覆盖ArrayController中的lookupItemController方法来配置阵列控制器来通过控制器代理来解析记录的方法。该方法应返回控制器的名称,而不使用controller后缀,即client而不是clientController。仅仅在数组控制器中设置 itemControllerClass 属性似乎不起作用。

  lookupItemController:function(object){
return'client';
},


解决方案

主人: https://github.com/emberjs/ember.js/commit/2a75cacc30c8d02acc83094b47ae8a6900c0975b



在撰写本文时,它不在任何发布的版本中。它很可能是1.0.0.pre.3的一部分。


I'm looping through a content of an ArrayController whose content is set to a RecordArray. Each record is DS.Model, say Client

{{# each item in controller}}
{{item.balance}}
{{/each}}

balance is a property of the Client model and a call to item.balance will fetch the property from the model directly. I want to apply some formatting to balance to display in a money format. The easy way to do this is to add a computed property, balanceMoney, to the Client object and do the formatting there:

App.Client = DS.Model({
 balance: DS.attr('balance'),

 balanceMoney: function() {
   // format the balance property
   return Money.format(this.get('balance');
 }.property('balance')
});

This serves well the purpose, the right place for balanceMoney computed property though, is the client controller rather than the client model. I was under the impression that Ember lookup properties in the controller first and then tries to retrieve them in the model if nothing has been found. None of this happen here though, a call to item.balanceMoney will just be ignored and will never reach the controller.

Is it possible to configure somehow a controller to act always as a proxy to the model in all circumstances.

UPDATE - Using the latest version from emberjs master repository you can configure the array controller to resolve records' methods through a controller proxy by overriding the lookupItemController method in the ArrayController. The method should return the name of the controller without the 'controller' suffix i.e. client instead of clientController. Merely setting the itemControllerClass property in the array controller doesn't seem to work for the moment.

lookupItemController: function( object ) {
        return 'client';
    },

解决方案

This was recently added to master: https://github.com/emberjs/ember.js/commit/2a75cacc30c8d02acc83094b47ae8a6900c0975b

As of this writing it is not in any released versions. It will mostly likely be part of 1.0.0.pre.3.

这篇关于强制控制器始终充当Ember中的模型的代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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