Emberjs 1.0:如何创建模型并以另一种方式显示它们
Emberjs 1.0: How to create Models and display them in another route?
我正试图在一条路由中创建一个集合("content"),并将它们传递到另一条路由以显示它们。这里怎么了?
错误:"StartView中的内容未定义"
这是代码
http://jsfiddle.net/insnet/cmBgz/21/
App = Ember.Application.create({LOG_TRANSITIONS: true});
App.ApplicationController = Ember.Controller.extend();
App.ApplicationView = Ember.View.extend({
templateName: 'application'
});
/* Routing */
App.Router.map(function() {
this.route("start", {path: '/'});
this.route("photos");
});
/* Start */
App.StartController = Ember.ArrayController.extend({
createModels: function() {
this.set('content', Ember.A());
this.addObject(Ember.Object.create({id: 1, title: 'Hello'}));
this.addObject(Ember.Object.create({id: 2, title: 'Digital'}));
this.addObject(Ember.Object.create({id: 3, title: 'World'}));
this.transitionToRoute('photos');
}
});
/* Photos */
App.PhotosView = Ember.CollectionView.extend({
contentBinding : 'App.StartController.content',
didInsertElement : function() {
console.info("content in StartView", this.get('content'));
}
});
<script type="text/x-handlebars" data-template-name="application">
<div class="contrainer">
<div class="hero-unit">
<h1>My App</h1>
{{outlet}}
</div>
</div>
</script>
<script type="text/x-handlebars" data-template-name="start">
<h2>View:Start</h2>
<button {{action "createModels"}} class="btn btn-primary">Create models and goto '/photos'</button>
</script>
<script type="text/x-handlebars" data-template-name="photos">
<h2>View:Photos</h2>
{{#each controller}}
<p>{{title}}</p>
{{/each}}
</script>
我将您的fiddle更新为工作版本:http://jsfiddle.net/mavilein/cmBgz/22/
起初,这是小提琴中的错误/误解:
1-这个绑定不起作用,因为你引用的是控制器类,而不是Ember框架创建的实例。
App.PhotosView = Ember.CollectionView.extend({
contentBinding : 'App.StartController.content',
2-您的视图中的日志消息是错误的,不能以这种方式工作。如果要访问视图的"基础内容",请始终使用属性"context"。术语"内容"仅与控制器一起使用。
/* Photos */
App.PhotosView = Ember.View.extend({
didInsertElement : function() {
console.info("content in StartView", this.get('context.content'));
}
});
这是您问题的可能解决方案:
a-这是一种可能的方式来查找startController的实例,并将其内容设置为照片路由的生成控制器的内容:
App.PhotosRoute = Ember.Route.extend({
model : function(){
var startController = this.controllerFor("start"); //in routes you have access to controllers with this method
return startController.get("content");
}
});
b-另一种可能性是手动声明路由的控制器,并使用Ember依赖注入(可能是最"余烬"的解决方案):
App.PhotosController = Ember.ArrayController.extend({
needs : ["start"], // "I need the startController plz!" -> accessible via "controllers.start"
})
/* The corresponding each in your template would look like this */
{{#each controller.controllers.start}}
相关文章:
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- jQuery:使用substr()的另一种方法
- 另一种显示和隐藏按钮的方式
- 单击()的另一种方式
- 解析一个复杂的JavaScript表达式,将其改写为另一种格式
- parseJSON在一种情况下有效,而在另一种情况中无效
- 如何访问对象's成员通过另一种方法填充的方法
- 是否可以在网页上用另一种字体设置jqmath-display的样式
- 用于自动将一种类型的URL更改为另一种类型
- 将日期字符串转换为另一种语言
- Rails 以一种方式格式化 DateTime.now 和 DateTime.yesterday 另一种方式 - 我如何
- 通过javascript将带有日期的字符串格式化为另一种格式
- 输入文本是't在一种情况下以相同的形式更新与另一种情况相同的角度模型
- 将JSON从一种格式转换为另一种格式
- 另一种方式是Javascript中的函数堆叠
- require.js是require的另一种方式
- 如何获得一种颜色的rgb值'It’它接近另一种颜色
- 在创建 toLowerCase 函数时,一种方式比另一种方式更好
- 检查窗口是否为弹出窗口的另一种方法
- Emberjs 1.0:如何创建模型并以另一种方式显示它们