Ember model Not Bind Dynamically using Link-to

时间:2017-06-04 23:51:54

标签: javascript ember.js ember-cli ember-2.0.0

I created an ember demo,A parent view and it's child this is the parent view

<h1>A list of Todo Tasks</h1>
<ul>
{{#each model as |todo|}}
<li>{{#link-to "todos.details" todo}}{{todo.desc}}{{/link-to}}</li>
{{/each}}
</ul>

{{outlet}}

and Its js login is import Ember from 'ember';

export default Ember.Route.extend({
    model (){
        return [{
            "id" : 1,
            "desc" : "Try use ember"
        },
        {
            "id" : 2,
            "desc" : "Add it to github"
        },
        ];

    }
});

but when i use the link-to and navigate the data didn't show unless i refresh the page This is the child view

<h2>The Details for <span style="color: green;">{{model.name}}</span> is : </h2>
{{#if model.error}}
<p>{{model.message}}</p>
{{else}}
<ul>
 {{#each model.steps as |task|}}
    <li>{{task.do}}</li>
    {{/each}}
</ul>
{{/if}}

{{outlet}} 

and its js logic

import Ember from 'ember';

export default Ember.Route.extend({
    model(params){

        if(params.id == "1"){
            return {
                name : "Ember SetUp",
                steps : [{
                    id :1,
                    do : "Download Ember Cli"
            },
            {
                    id :2,
                    do : "Generate New Project"
            },
            {
                    id :3,
                    do : "Generate Pages&Routes"
            }
                ]
            };
        }
        else{
            return {
                error : true,
                name : "Not Found",
                message : "There is no task with this id"
            }
        }
    }
});

iam using ember 2.5 and this is part of the router

this.route('todos', function() {
    this.route('details',{path : "/:id"});
  });

2 个答案:

答案 0 :(得分:2)

{{#link-to "todos.details" todo}}

由于您提供了对象todo,因此它不会执行模型钩子。 所以试试

{{#link-to "todos.details" todo.id}}

请参阅此处:https://guides.emberjs.com/v2.13.0/routing/specifying-a-routes-model/#toc_dynamic-models

  

注意:具有动态段的路径将始终具有其模型挂钩   通过URL输入时调用。如果输入路线   转换(例如,当使用链接到Handlebars帮助程序时)和a   提供了模型上下文(链接到的第二个参数),然后是钩子   没有执行。如果提供了标识符(例如id或slug)   相反,然后将执行模型钩子。

答案 1 :(得分:1)

Ack,OP视频在这里。对于那个很抱歉。我的小错误说法,我应该在视频的评论中解决这个问题并尝试修改它。对困惑感到抱歉! d: