【问题标题】:Ember.Object computed property returning an objectEmber.Object 计算属性返回一个对象
【发布时间】:2013-01-31 09:47:56
【问题描述】:

所以我有一个 Ember 对象:

App.User = Ember.Object.extend({
    firstName: null, 
    lastName: null, 
    dob: Ember.Object.create({
        day: null,
        month: null,
        year: null,
        display: function () {
            var self = this,
            day = (self.get('day') < 10 ? "0" : "") + self.get('day'),
            month = (self.get('month') < 10 ? "0" : "") + self.get('month'),
            year = self.get('year');

            return day + '/' + month + '/' + year;
        }.property('day', 'month', 'year')
    })
});

在我的控制器上——我正在创建对象的一个​​版本——然后被一个表单绑定:

App.IndexController = Em.ArrayController.extend({
    user: App.User.create()
});

但是 - 出于某种原因,每当我尝试在我的控制器中或在我的视图中获取 dob.display 计算机属性时,我只会返回一个对象。

this.get('user.dob.display')

{{user.dob.display}}

任何想法如何确保它返回一个字符串?这一直有效,直到几天前我从 1.0.0-pre2 更新到 1.0.0-pre4。

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    您不应使用create,而应使用createWithMixins。这是在创建对象时定义计算属性的唯一方法:

    dob: Ember.Object.createWithMixins({
        day: null,
        month: null,
        year: null,
        display: function () {
            day = (this.get('day') < 10 ? "0" : "") + this.get('day'),
            month = (this.get('month') < 10 ? "0" : "") + this.get('month'),
            year = this.get('year');
    
            return day + '/' + month + '/' + year;
        }.property('day', 'month', 'year')
    })
    

    注意一点:在extend方法中设置属性时,每个实例共享同一个对象。 如果你不知道,go read "Understanding Ember.Object" article written by Dan Gebhardt(这篇文章可能有点过时了,但想法还是一样的)。

    因此,在这里,您有:

    var user1 = App.User.create().get("dob").set("year", 1988);
    var user2 = App.User.create();
    console.log(user2.get("dob.year")); // => 1988
    

    如果要定义实例之间不同的对象,则必须像这样重写init 方法:

    App.User = Ember.Object.extend({
        init: function() {
            this._super(); // do not forget that
            this.set('dob', Ember.Object.createWithMixins({
                // [code ommitted]
            }));
        }
    });
    

    但我建议你创建一个模型dob,而不是每次创建用户时都创建它。

    你可以试试this in a JSFiddle

    【讨论】:

      【解决方案2】:

      在 pre4 中,您不能在已创建的 Ember.Object 上定义计算属性。

      不允许

      dob: Ember.Object.create({
          day: null,
          month: null,
          year: null,
          display: function () {
              var self = this,
              day = (self.get('day') < 10 ? "0" : "") + self.get('day'),
              month = (self.get('month') < 10 ? "0" : "") + self.get('month'),
              year = self.get('year');
      
              return day + '/' + month + '/' + year;
          }.property('day', 'month', 'year')
      })
      

      只允许对扩展的对象执行此操作。

      我创建了一个适用于你的 dob 逻辑的小提琴(abit 修改)。

      http://jsfiddle.net/Energiz0r/AeDAw/4/

      【讨论】:

      • 谢谢,但是我的 d/m/y 属性绑定到视图上的某些表单字段,所以如果这些值在 setUpControllers 后发生更改,这会起作用吗?
      • @TommyKey 这不对,你只需要使用createWithMixins 而不是create,看看我的回答
      • 是的,没错,但我不想提及它,因为我觉得你不应该在另一个扩展的对象中创建(withMixins)对象。
      猜你喜欢
      • 2017-06-07
      • 2019-07-01
      • 1970-01-01
      • 2014-06-18
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      • 2018-10-13
      • 2021-07-15
      相关资源
      最近更新 更多