gpt4 book ai didi

javascript - Emberjs 1.0 : How to create Models and display them in another route?

转载 作者:行者123 更新时间:2023-11-30 13:07:58 24 4
gpt4 key购买 nike

我正在尝试在一条 route 创建一个集合('content')并将它们传递到另一条路线以显示它们。这里有什么问题吗?

Error: "content in StartView undefined"

这是代码

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/

起初,这是 fiddle 中的错误/误解:

1 - 此绑定(bind)不起作用,因为您引用的是 Controller 类,而不是 Ember 框架创建的实例。

App.PhotosView = Ember.CollectionView.extend({
contentBinding : 'App.StartController.content',

2 - 您 View 中的日志消息是错误的,不能那样工作。如果您想访问 View 的“底层事物”,请始终使用属性“上下文”。术语内容仅与 Controller 结合使用。

/* Photos */
App.PhotosView = Ember.View.extend({
didInsertElement : function() {
console.info("content in StartView", this.get('context.content'));
}

});

以下是您问题的可能解决方案:

a - 这是查找 startController 实例并将其内容设置为照片路由生成的 Controller 内容的可能方法:

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 - 另一种可能性是手动为路由声明 Controller 并使用 Ember 依赖注入(inject)(可能是最“emberish”的解决方案):

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}}

关于javascript - Emberjs 1.0 : How to create Models and display them in another route?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14969353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com