2012-06-08 48 views

回答

22

只需使用this.get('theProperty')

例子:

var data = { 
    foo: "hello", 
}; 

var MyModel = Em.Object.extend({ 
    init: function() { 
     this._super(); 
     var foo = this.get('foo'); 
     alert(foo); 
    } 
}); 

MyModel.create(data); 
0

使用閉包並創建一個新的init函數,將封閉的參數傳遞給它的原型init函數。而且,這樣你最終不會覆蓋敏感的屬性,例如方法。 注意:init在構造函數設置所有屬性後調用

Class = Ember.Object.extend({ 
init:function(response){ 
    console.log(this.get("msg")+this.get("msg_addressee")+"?"); 
    console.log(response); 
}, 
msg:"SUP, " 
}); 

var arg = "not much."; 

obj = Class.create({ 
init:function(){ 
    console.log("output:"); 
    this._super(arg); 
    console.log("indeed, "+arg); 
}, 
msg_addressee:"dude" 
}); 

//output: 
//SUP, dude? 
//not much. 
//indeed, not much.