2012-06-08 67 views

Trả lời

22

Chỉ cần sử dụng this.get('theProperty')

Ví dụ:

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

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

MyModel.create(data); 
0

Sử dụng các bao đóng và tạo một hàm init mới chuyển đối số đã đóng cho hàm init nguyên mẫu của nó. Ngoài ra, theo cách này, bạn không kết thúc việc ghi đè các thuộc tính nhạy cảm, ví dụ như các phương thức. lưu ý: init được gọi sau khi tất cả các thuộc tính được thiết lập bởi hàm tạo

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.