我可以额外计算的属性添加到灰烬ArrayProxy? [英] Can I add an additional computed property to an Ember ArrayProxy?

查看:140
本文介绍了我可以额外计算的属性添加到灰烬ArrayProxy?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,有一个有点时髦设置的遗留灰烬应用程序,我试图清理东西,并按照约定的更多一点。一个问题是,而不是从模式索引路线的挂钩返回一个数组,我们返回一个包含数组的对象。所以,我包裹模式 ArrayProxy setupController 是这样的:

I'm working on a legacy Ember app that has a bit of a funky setup and I'm trying to clean things up and follow conventions a bit more. One issue is that, rather than returning an array from the model hook of an index route, we're returning an object that contains an array. So, I'm wrapping the model in an ArrayProxy in setupController like this:

setupController: (controller, model) ->
  model_proxy = Ember.ArrayProxy.create({content: model.get('item')})
  controller.set('content', model_proxy)

这实际上有效(即内容当AJAX承诺解决和更新 model.item 装入数据)。问题是,有关于模式,我还需要在我的控制器其他属性。 模式的时候承诺解决其初始化为true,然后设置为false needsLoader 属性。我们使用它来显示一个微调给用户,而数据正在从服务器获取。

This actually works (i.e. content is updated when the AJAX promise resolves and model.item is loaded with data). The problem is, there's another property on model that I also need in my controller. model has a needsLoader property which is initialized to true and then set to false when the promise resolves. We're using this to show a spinner to the user while the data is being fetched from the server.

所以,我的问题是:有没有什么办法,我可以代理 needsLoader ArrayProxy

So, my question is: is there any way that I can proxy needsLoader in the ArrayProxy?

一个解决方法我都试过是挂钩原始模式到一个非标准的方式控制器:

One solution I have tried is to hook the original model onto the controller in a non-standard way:

setupController: (controller, model) ->
  ....
  controller.set('_model', model)
  ....

这让我通过调用访问 needsLoader 从控制器 @get('_ model.needsLoader')。它的工作原理,但我喜欢做最脏最累的工作,在路由器,所以我有一个干净的界面在我的控制器只是调用模式如常。

This lets me access needsLoader from the controller by calling @get('_model.needsLoader'). It works, but I'd like to do all of the dirty work in the Router so that I have a clean interface in my controller to just call model as usual.

谢谢!

推荐答案

如果它是有道理的,但你可以创建自己的类型不确定:

Not sure if it makes sense, but you can create your own type:

var myArrayProxy = Ember.ArrayProxy.extend({
  countPlusTen: function(){
    return this.get('content.length') + 10;
  }.property('content.length')
});


var instance = myArrayProxy.create({
  content: [1,2,3]
});

console.log(instance.get('countPlusTen'));

例如: http://emberjs.jsbin.com/novavuqi/1/edit

这篇关于我可以额外计算的属性添加到灰烬ArrayProxy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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