MVC框架始终允许将视图存储为单独的文件并进行检索.这应该如何在Ember.js中完成?我一直在寻找几个小时,所以如果你想把它作为某种重复投票,请至少链接到一个提供明确,直截了当的答案的问题或资源.
例如,Ember会自动创建一个索引控制器,并且index只要你指定了一个{{outlet}}url就自动呈现模板/,所以除了window.App = Ember.Application.create();我的app.js 之外我不需要任何代码来实现这个功能.如何创建一个单独的文件,如index.handlebars并让Ember查找并呈现它?
我有一个应用程序,目前包含相同模型的对象的视图.它们从服务器检索,循环并使用add方法添加到列表控制器
<script>
App.Controllers.List = Em.ArrayProxy.create({
content: Ember.A([]),
add: function(obj){
this.pushObject(obj);
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
我现在正在开发一个用户创建新对象的部分(通过验证后)将添加到列表中并发送到服务器.
我找不到关于通过输入表单创建新对象的最佳模式的任何示例.我可以看到一些选项,并且半实现了一些,但没有任何感觉是正确的.
我可以解决我想要的功能,但我更愿意确保我了解最佳实践.
我目前有这样的东西(这是我列表中的第二个子弹)
<script>
App.Views.ItemCreate = Em.View.extend({
content: App.Models.Item.create({}),
templateName: 'create',
createButton: function(){
var itemObj = this.get('content');
var item = {};
item.title = this.get('content').get('title');
$.ajax({
type: 'POST',
url: '/test/data.json',
data: item,
dataType: 'json',
success: function(responseData, textStatus, jqXHR) {
App.Controllers.List.add(itemObj);
}
});
}
});
</script>
<script type="text/x-handlebars" data-template-name="create">
{{view Em.TextField id="create-title" valueBinding="content.title"}}
<a href="#" {{action "createButton" }}>Create</a>
</script>
Run Code Online (Sandbox Code Playgroud)
任何帮助非常感谢
笔记
我已经改变了pangratz的正确答案.虽然其他回复直接回答了我的问题,但我相信那些通过Google找到这个问题的人应该参考Pangratz提供的答案,因为它不仅是好的MVC,而且更像是Ember-y:o)
我有一个individualStore(从Em.ArrayController扩展),其任务是保留一个单独的对象数组.我的应用程序调用了几个API,它们返回发送到商店的单个对象.将其视为我的应用程序中缓存的单个记录的数据库.
App.individualStore = App.ArrayController.create({
allIndividuals: function () {
return this.get('content').sort(function (a, b) {
return (b.votes_count - a.votes_count);
});
}.property('@each.votes_count').cacheable(),
aliveIndividuals: function () {
return this.get('content').filter(function (individual) {
return (!!individual.living);
}).sort(function (a, b) {
return (b.votes_count - a.votes_count);
});
}.property('@each.living', '@each.votes_count').cacheable(),
deceasedIndividuals: function () {
return this.get('content').filter(function (individual) {
return (!individual.living);
}).sort(function (a, b) {
return (b.votes_count - a.votes_count);
});
}.property('@each.living', '@each.votes_count').cacheable()
});
Run Code Online (Sandbox Code Playgroud)
我的观点有一个`individualBinding:'App.individualStore.allIndividuals',它按照预期完美呈现.
我想添加过滤按钮,例如Show: All | Alive | Deceased.在这里更改过滤的正确方法是什么?请记住,无论标准是什么,我都希望它始终与individualStore保持同步.
有人建议在运行时更改绑定,
this.bind('users', Ember.Binding.from('App.individualStore.aliveIndividuals'));
Run Code Online (Sandbox Code Playgroud)
这在我对这些按钮的前两次三次点击中有效,但随后会冻结浏览器(有点无限循环?).
这对我来说也不是最好的选择.我是新来的,所以你说的任何东西都会有所帮助.提前致谢.