Passing arguments to init in ember.js

前端 未结 2 1027
陌清茗
陌清茗 2021-02-18 16:18

How can I pass arguments to init() or access the arguments passed to create() inside init() in ember.js

相关标签:
2条回答
  • 2021-02-18 16:43

    Just use this.get('theProperty')

    Example:

    var data = {
        foo: "hello",
    };
    
    var MyModel = Em.Object.extend({
        init: function() {
            this._super();
            var foo = this.get('foo');
            alert(foo);
        }
    });
    
    MyModel.create(data);
    
    0 讨论(0)
  • 2021-02-18 16:44

    Use closures and create a new init function that passes the closed argument to its prototype init function. Also, this way you don't end up overwriting sensitive properties, like methods for example. note: init is called after all the properties are set by the constructor

    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.
    
    0 讨论(0)
提交回复
热议问题