2014-12-07 20 views
0

我正在使用Ember.js中的對象模型。我非常喜歡它,但有一件事讓我感到困惑。在Ember.js中擴展對象控制器中斷

此作品(http://emberjs.jsbin.com/jepazo/1/edit?html,css,js,console,output):

App.Car = Ember.Object.extend({ 
     color: 'red', 
     brand: "Tesla", 
     desciption: function(){ 
      return this.get('color') + this.get('brand'); 
     }.property('color','brand') 
}) 


App.IndexController = Ember.Controller.extend({ 
    zl: [App.Car.create(), App.Car.create({brand: 'BMW'})] 
}) 

但我不能宣佈它的控制器(http://emberjs.jsbin.com/jepazo/2/edit?html,css,js,console,output)上:

App.IndexController = Ember.Controller.extend({ 
    car: Ember.Object.extend({ 
     color: 'red', 
     brand: "Tesla", 
     desciption: function(){ 
     return this.get('color') + this.get('brand'); 
     }.property('color','brand') 
    }), 
    zl: [this.get('car').create(), this.get('car').create({brand: 'BMW'})] 
)}; 

有誰知道爲什麼會這樣?

+0

在你的問題中的代碼看起來是正確的,在你的提琴代碼缺少一個'}); '。 – givanse 2014-12-07 00:20:36

回答

3

這是因爲你的數組聲明中的「this」是Window對象。

如果您將zl轉換爲返回數組的它將起作用的computedProperty。

喜歡的東西:

zl: function() { 
     return [this.get('car').create(), this.get('car').create({ brand: 'BMW'})]; 
    }.property() 

下面是說明這一問題的jsbin:

http://emberjs.jsbin.com/fudiruduxa/1/edit?html,css,js,output

+0

當然這是有道理的。謝謝你的幫助 :) – Paul 2014-12-08 21:04:31