Passing arguments to init in ember.js

前端 未结 2 1028
陌清茗
陌清茗 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: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.
    

提交回复
热议问题