21
初始化如何傳遞參數給init()
或訪問傳遞給create()
的參數內init()
在ember.js參數傳遞到ember.js
初始化如何傳遞參數給init()
或訪問傳遞給create()
的參數內init()
在ember.js參數傳遞到ember.js
只需使用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);
使用閉包並創建一個新的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.