ember.js 1.0 将 Controller 绑定(bind)到 Controller

标签 ember.js

或者将 Controller 绑定(bind)到两个不同的 View 。

我这样做是为了让我的 'home' View 获取 PostsController 的内容..但我需要真正的方法来做到这一点,如果你能帮忙的话请我!

HomeController: Ember.ArrayController.extend({    
     init: function() {
        setTimeout(function() {
           App.router
             .get('homeController')
               .set('content', App.router.get('postsController').get('content'));
           }, 0);
        }

})

(此时使用最新版本SHA:83542a6)

最佳答案

我不太明白你的问题。下面的解决方案有什么问题? (original jsFiddle here) (updated jsFiddle here)

(更新:添加骨架路由器)

JavaScript/Ember:

// Application
App = Ember.Application.create({
    ApplicationController: Ember.Controller.extend(),
    ApplicationView: Ember.View.extend({
        templateName: "application-view",
    }),
    Router: Ember.Router.extend({
        // initialState of Ember.Router is "root"
        root: Ember.Route.extend({
            // "index" can be called whatever we want
            index: Ember.Route.extend({
                route: '/',
                enter: function(router) {
                    console.log("entering root.index from", router.get('currentState.name'));
                },
                connectOutlets: function(router) {
                    console.log("entered root.index, fully transitioned to", router.get('currentState.path'));
                    // Demo: read and write from router's connectOutlets
                    console.log("Router says, A:", App.get("aController").get('content'));
                    console.log("Router says, B:", App.get("bController").get('content'));
                    App.get("aController").pushObject({name: "Network switch", color: "beige", type: "Cisco"});
                    console.log("Router says, A:", App.get("aController").get('content'));
                    console.log("Router says, B:", App.get("bController").get('content'));
                }
            })
            // ... 
            // (add routes here)
            // ...
        })
    })
});

// Controllers
App.aController = Ember.ArrayController.create({
    content: [],
    addContact: function(contact) {
        this.content.push(contact);
    }
});

App.bController = Ember.Object.create({
    contentBinding: "App.aController.content"
});

// Demo: Change a (shows up in b)
App.aController.set('content', [
    {name: "Apple", color: "red", type: "fruit"},
    {name: "Banana", color: "yellow", type: "fruit"},
    {name: "Sports car", color: "black", type: "vehicle"},
    {name: "Sun", color: "white", type: "star"},
     ]);

// Demo: Change b (shows up in a)
(function() {
    var temp = App.aController.get('content');
    temp.push({name: "Linus", color: "n/a", type: "human"});    
    App.bController.set('content', temp);
})();

HTML/ Handlebars :

<script type="text/x-handlebars" data-template-name="application-view">
<div id="my-app">
<h1>aController</h1>
{{#collection tagName="ul" contentBinding="App.aController.content"}}
    {{#with view.content}}
        Name: {{name}}, Color: {{color}}, Type: {{type}},
    {{/with}}
{{/collection}}
<h1>bController</h1>
{{#collection tagName="ul" contentBinding="App.bController.content"}}
    {{#with view.content}}
        Name: {{name}}, Color: {{color}}, Type: {{type}},
    {{/with}}
{{/collection}}
</div>
</script>

关于ember.js 1.0 将 Controller 绑定(bind)到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12782287/

相关文章:

ember.js - Ember JS,更新数组未反射(reflect)在 View 中

model-view-controller - Controller 中应包含哪些逻辑以及路由中应包含哪些逻辑?

javascript - 当 Ember 模板更改时运行 jQuery

javascript - 使用 Fixture Adapter 将 js 对象切换到 Ember Data 模型并不断收到 "Cannot call method ' findAll' of undefined"错误

ember.js - Ember Data : CreateRecord not updating model when using store. 使用查询参数查找

javascript - 如何过滤 Ember.js 中的本地 hasMany 记录集

javascript - 如何检查(断言)Ember Promise 是否已结算?

ember.js - 获取 ember 组件来维护跨路由转换的状态

javascript - 转换到路由时 URL 中的 "undefined"

ember.js - 如何在 ember cli 应用中使用第三方 npm 包