21

如何将init()参数传递给或访问传递给ember.jscreate()内部的参数init()

4

2 回答 2

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);
于 2012-06-08T20:52:45.867 回答
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.
于 2012-07-02T15:46:15.760 回答