简体   繁体   中英

Ember.js Calling model's method from controller

Is there any way to get a hand on the model from a controller? I would need to call an instance method defined on the model and

this.get(methodName)(params)

does not work, as it loses the 'this' call context within the called function.

Thank you.

I'm not sure which context you want to exist in the method, if you need to switch the context you can use call/apply. The context should be the context of the method, not the controller by javascript standards.

var model = this.get('model');

model.methodName(arg1, arg2);

Controller's context

model.methodName.apply(this, arrayOfArgs);

model.methodName.call(this, arg1, arg2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM