Ember用参数计算属性 [英] Ember computed properties with arguments

查看:101
本文介绍了Ember用参数计算属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以向计算的属性添加参数。到目前为止,我尝试的一切都导致错误,并没有发现任何事情。我想使用不包含在我的模型中的值来构建一个URL。

I was wondering if it was possible to add arguments to a computed properties. So far, everything I tried resulted in errors and found nothing on the subject. I want to build a URL using a value that is not included in my model.

我正在寻找一些看起来像这样的东西:

I'm looking for something that would look like this :

// App.js
App.Image = DS.Model.extend({
    image_path_sh: DS.attr(), // image.jpg
    image_size_nm: DS.attr(), // 234234
    image_alt_sh: DS.attr(), // My image
    image_abs_url: function(width, height) {
        return "http://localhost/images/" + this.get('image_path_sh') + "/" + width "x" + height
    }.property('image_path_sh')
});

// index.html
// I know this doesn't work, but I'd like to have something that easy to use
{{#each image}}
    <img src="{{image_abs_url 250 250}}" alt="{{image_alt_sh}}" />
{{/each}}

我的服务器将返回一个调整大小的图像。我不想将它放在我的数据库中,因为这些不是固定的值。

My server will return an image that is resized. I don't want to put that in my database because these are not fixed value.

推荐答案

计算属性不应该依赖关于参数,它打破了缓存范式,这正是帮助者和方法。

A computed property shouldn't rely on parameters, it breaks the caching paradigm, that's precisely what helpers and methods are for.

Ember.Handlebars.helper('img', function(prop, height, width, options) {
  return new Handlebars.SafeString('<div style="height:' + height +'px;width:'+ width +'px;background-color:' + prop + '">' + prop + '</div>');
});

http://emberjs.jsbin.com/IgUFaTAk/1/edit

这篇关于Ember用参数计算属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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