我希望看到内容当我点击导航时使用Ember.Js

时间:2013-07-19 01:08:40

标签: jquery templates model ember.js ember-data

定义模板

<script type="text/x-handlebars" id="application">
    {{outlet}}
</script>

<script type="text/x-handlebars" id="markets/sources">
    {{#each model }}
        <span>{{source_channel}}</span>
        <span>{{handle}}</span>
    {{/each}}

</script>

<script type="text/x-handlebars" id="markets">
    <div class="leftside">
    {{#each model }}
        <span>{{name}}</span>
        <span>{{created}}</span>
        {{#linkTo 'markets.sources' this class="link" }}<span>go to</span>{{/linkTo}}  
    {{/each}}
    </div>
    <div class="rightside">
        {{outlet}}
    </div>
</script> 

定义路线

var App = Ember.Application.create();

App.Router.map(function() {
    this.route("index", {path: "/"});                                                       
    this.resource('markets', {path: "/markets"}, function() {
        this.route("sources", { path: "/:markets_id" });
    });
});

App.IndexRoute = Ember.Route.extend({
    redirect: function() {
        this.transitionTo('markets');
    }
});

App.MarketsRoute = Ember.Route.extend({
    model: function () {
        return App.Markets.find();
    }
});

App.MarketsSourcesRoute = Ember.Route.extend({
    model: function(){
        return App.Sources.find();
    },
    serialize: function(model) {
        return { markets_id: model.id };
    }
});

定义模型

App.Store = DS.Store.extend({
   revision: 12,
   adapter: DS.FixtureAdapter
});

App.Markets = DS.Model.extend({
    name: DS.attr("string"),
    created: DS.attr("string")
});

App.Sources = DS.Model.extend({
   source_channel: DS.attr("string"),
   handle: DS.attr("handle")
});

App.Sources.FIXTURES = [
    {id:1, markets_id:"1310", source_channel:"sc1", handle: "hn1"},
    {id:2, markets_id:"1310", source_channel:"sc2", handle: "hn2"},
    {id:3, markets_id:"1310", source_channel:"sc3", handle: "hn3"},
    {id:4, markets_id:"1512", source_channel:"sc4", handle: "hn4"},
    {id:5, markets_id:"1512", source_channel:"sc5", handle: "hn5"}
];

App.Markets.FIXTURES = [
    {id:"1310", name:"test1", created:"2012-2-3"  },
    {id:"1320", name:"test2", created:"2012-2-13"  },
    {id:"1512", name:"test3", created:"2012-2-23"  }
]; 

定义控制器

App.MarketsController = Ember.ObjectController.extend({}); 
App.MarketsSourcesController = Ember.ObjectController.extend({});

我希望看到子内容当我使用EmberJs点击{{#linkTo}}时

在这里,我看不到我想要的结果._ 当我点击左侧的一些锚标签时,它会在右侧显示黑色内容 我认为,“来源”模型没有与“市场/ sor”模板相结合 当我在leftside中单击锚标记时,我希望看到源模型的正确结果。

如果有可能,我希望在jsbin或jsfiddle上看到结果

1 个答案:

答案 0 :(得分:0)

踢腿和咯咯笑的随机信息。

详细信息1,路由与其父资源共享相同的资源。

详细信息2,当您设置链接时,您要发送与该路线相关的模型或该模型的ID。

详细信息3,因为您在市场和资源之间存在一对多的关系,您需要在ember数据中设置一些额外的查询助手(因此您可以通过markets_id查询而不是源的ID)< /强>

所有人都说,这里有一个jsbin与它一起工作(虽然不是花哨,我不为自己的css技能感到自豪)

jsbin