前言
有关基于第一个答案和jsbin的更新代码,请参见Edit#1。
我正在尝试将Bootstrap模型集成到Ember应用程序中。我有一个名为main modal content的id,希望将模板写入该id。我有以下控制器:
App.InventoryItemController = Ember.ObjectController.extend({
actions:{
showModal: function(){
var item=this.get('model');
var source = $("#ii-edit").html();
var template = Handlebars.compile(source);
var html_str = template(item);
$('#main-modal-content').html(html_str);
$('#myModal').modal();
},
saveInventoryItem: function(){
alert('now to save!!!');
}
},
isExpanded:false
});][1]
并且该模板:
<script type="text/x-handlebars-template" id="ii-edit">
here is my edit {{id}} <div {{action 'saveInventoryItem' target='InventoryItemController'}}>save inventory item</div>
</script>
但我得到以下错误:
小时
如何将对目标的引用设置回InventoryItemController,以便调用saveInventoryItem?此外,这只是第一次通过——这看起来是在Ember中进行模态转换的合理方式吗?
编辑#1
下面是一个尝试维护对开放模式的引用的示例:http://jsbin.com/tabor/2/edit
我已经用这个更新了一个应用程序路由,但不清楚如何为有问题的特定控制器设置模型-或者这个路由需要返回模型?:
App.ApplicationRoute = Ember.Route.extend({
actions: {
openModal: function(modalName, object) {
this.controller.set('model', object); // <- this is not working
this.set('model',object); // <- this also isn't working
this.render(modalName, {
into: 'application',
outlet: 'modal',
controller: 'modal'
});
if($('#myModal')){
$('#myModal').modal('show');
}
console.log('after this');
},
closeModal: function() {
//return this.disconnectOutlet({
this.disconnectOutlet({
outlet: 'modal',
parentView: 'application'
});
$('#myModal').modal('hide');
}
},
});
将其添加到上述路由中没有帮助:
// even hardcoding this isn't working!!!!
model:function(){
//return this.get('object');
return Em.Object.create({id: 7, name: 'Jon'});
}
模板:
<script type="text/x-handlebars" data-template-name="myModal">
<div>
id val: {{id}}
<button {{action 'sayHello' id }}>Say Hello</button>
</div>
</script>
1条答案
按热度按时间35g0bw711#
查看the cookbook,了解更简单的方法。
它包括在应用程序模板中的某个位置使用
modal
出口,并使用路由的render
方法将模板插入其中: