链接标签dosen'我不能在Emberjs中工作

Link tag dosen't work in Emberjs

本文关键字:不能 Emberjs 工作 标签 dosen 链接      更新时间:2023-09-26

这是型号

App.Store = DS.Store.extend({
   revision: 12,
   adapter: DS.FixtureAdapter
});
App.Markets = DS.Model.extend({
    ids: DS.attr("string"),
    name: DS.attr("string"),
    created: DS.attr("string")
});
App.Markets.FIXTURES = [
    {ids:"312", name:"joy", created:"2012/1/1"},
    {ids:"412", name:"adel", created:"2012/1/2"},
    {ids:"512", name:"john", created:"2012/1/3"}
]; 
App.Sources = DS.Model.extend({
   source_channel: DS.attr("string"),
   handle: DS.attr("handle")
});
App.Sources.FIXTURES = [
    {source_channel:"sc1", handle: "hn1"},
    {source_channel:"sc2", handle: "hn2"}
]; 

这是路线。

var App = Ember.Application.create();
App.Router.map(function() {
    this.resource('markets', {path: '/markets'}, function() {
        this.resource("sources", { path: "/:market_id" });
    });                                                          
});
App.MarketsRoute = Ember.Route.extend({
    model: function () {
        return App.Markets.find();
    }
});
App.SourcesRoute = Ember.Route.extend({
    model: function(){
        return App.Sources.find();
    }
});

这是模板

<script type="text/x-handlebars" id="_sources">
  {{#each sources in content}}
    <span>{{sources.handle}}</span>
    <span>{{sources.sources_channel}}</span>
  {{/each}}
</script>
<script type="text/x-handlebars" id="markets">
    {{#each markets in content }}
        {{#linkTo 'sources' markets.ids class="test" }}<span>Source</span>{{/linkTo}}
        <span>{{markets.name}}</span>
        <span>{{markets.created}}</span>
    {{/each}}
    <div class="sources">
        {{partial "sources"}}
    </div>
</script>  

当我进入/#/markets时,我可以看到市场列表。这是正确的。

专注于市场模板的{{#linkTo 'sources' markets.ids class="test" }}Source{{/linkTo}}

在这里,markets.ids不起作用。我想在单击链接时转到/#/markets/markets_id

将对象传递到linkTo中的sources路由时,

App.SourcesRoute中定义serialize方法。

模板:

{{#linkTo 'sources' markets class="test" }}<span>Source</span>{{/linkTo}}

路线:

 App.SourcesRoute = Ember.Route.extend({
    model: function(){
        return App.Sources.find();
    },
    serialize: function(model) {
        return { market_id: model.ids };
    }
});

您需要使用linkTo 'sources' markets。即:将每个循环中的模型传递给`linkTo。

注意:关于您的命名约定。Ember喜欢模型是单数,而Routes/Controllers是复数还是单数,这取决于路由是否指向一个或多个模型。

编辑:澄清

linkTo更改为此,

{{#linkTo 'sources' markets class="test" }}<span>Source</span>{{/linkTo}}