如何在emberjs渲染方法中传递参数? [英] How to pass parameters in emberjs render method?
问题描述
我在尝试将参数传递给模态视图,同时呈现它。这是我到目前为止所得到的:
I am trying to pass parameters to a modal view while rendering it. Here is what I got so far:
在我的应用程序路由中,我有:
In my application route, I have:
export default Ember.Route.extend({
actions: {
openModal: function(modal, opts) {
return this.render(modal, {
into: 'application',
outlet: 'modal'
model: function(){ return opts },
controller: modal
});
},
closeModal: function() {
return this.disconnectOutlet({
outlet: 'modal',
parentView: 'application'
});
}
}
});
我有一个一般的模式视图:
I have a general modal view:
import Ember from 'ember';
export default Ember.View.extend({
tagName: 'div',
classNames: 'modal'.w(),
didInsertElement: function() {
console.log("did insert element");
this.$().attr('id', 'modal');
this.$().modal({
keyboard: false,
backdrop: 'static'
});
return this.$().modal('show');
},
willDestroyElement: function() {
return this.$().modal('hide');
}
});
和特定于我的模式的视图:
and a view specific to my modal:
import ModalView from '../modal';
export default ModalView.extend();
我想要'opts'(作为openModal中的参数传递)进行处理,以便我可以填写根据它的模态的内容。到目前为止,我不能从我的模式控制器得到任何东西:
I want 'opts' (passed as an argument in openModal) to be processed so that I can fill the content of the modal depending on it. So far I cannot get anything from my modal controller:
import ModalController from '../modal'
export default ModalController.extend({
init: function(){
console.log('initializing modal');
console.log(this.get('model'));
console.log(this);
},
actions: {
confirm: function() {
//alert('OK, it will be done!');
return this.send('closeModal');
}
}
});
这样做是正确的方法?
推荐答案
所以我想出来,必须做一个更好的做法,但以下内容应该适用于我:
So I figured it out, there has to be a better to do it but the following should work for me:
申请路线:
import Ember from 'ember';
export default Ember.Route.extend({
actions: {
openModal: function(modal, opts) {
this.controllerFor(modal).set('model', opts);
return this.render(modal, {
into: 'application',
outlet: 'modal'
});
},
closeModal: function() {
return this.disconnectOutlet({
outlet: 'modal',
parentView: 'application'
});
}
}
});
我的模态窗口的控制器:
Controller of my modal window:
import ModalController from '../modal'
export default ModalController.extend({
modalContent: "test",
actions: {
confirm: function() {
//alert('OK, it will be done!');
return this.send('closeModal');
}
},
idUpdated: function(){
var id = this.get('model').id;
console.log(id);
// Update modal content with the id here
// Ajax request goes here.
}.observes("id")
});
这篇关于如何在emberjs渲染方法中传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!