Emberjs, Target a model without leaving '/models' url

走远了吗. 提交于 2019-12-24 01:11:13

问题


I have a table of models and I want to be able to click my {{action 'edit'}} in one of those model's and ember will know which model # the {{action 'edit'}} is in.

Right now I am receiving this error: 'GET localhost:3000/category/undefined 404 (Not Found) '

Here is my code in the '/categories' route.

<tbody>
    {{#each}}
      <tr class="people-list">
        <td>
            <input type="checkbox" class="toggle">
            {{#if isEdit}}
            {{view Ember.TextField valueBinding="Name" name="Name"}}
            {{else}}
            <label class="category-text">{{#linkTo 'category' this}}{{Name}}{{/linkTo}}</label>
            {{/if}}
            <img class="table-img" src="images/x.png">
            <img class="table-img" {{action 'edit'}} src="images/pencil-grey.png">
            </td>
        </tr>
    {{/each}}
</tbody>

And then on the controller:

actions: {
    edit:function(){
        this.set('isEdit', true);
    }
}

the 404 occurs when i click

<img class="table-img" {{action 'edit'}} src="images/pencil-grey.png">

and the model

VpcYeoman.Category = DS.Model.extend({
    RecordCategoryID:DS.attr('number'),
    Name: DS.attr('string'),
    ProjectName: DS.attr('string'),
    WorkDescription: DS.attr('string'),
    workflow: DS.belongsTo('category', {
        polymorphic: true
    }),
    classNameBindings: ['isAdministrator']
});

VpcYeoman.Calendar.FIXTURES = [

  {
    id: 0,
    RecordCategoryID: 0,
    Name: "Fire",
    ProjectName: "Electra"
  },

  {
    id: 1,
    RecordCategoryID: 1,
    Name: "Water",
    ProjectName: "Nike"
  }

];

and router.js

  this.resource('categories', { path: '/categories' });
  this.resource('category', { path: '/category/:category_id' },function() { 
    this.route('edit');
  });

I wasn't sure if i should have added anything about 'edit' to the router because i did not want the program to have to change URLs in order to focus onto a specific model.


回答1:


There is a pretty simple solution, just send the model into the action.

<img class="table-img" {{action 'edit' this}} src="images/pencil-grey.png">

actions: {
  edit: function(model){
    model.set('isEdit', true);
  }
}

The undefined is coming from Ember trying to generate the url when you click the link-to. It's taking the model then looking for category_id on the model to build the unique url. Alas, you don't have that property on the model, so you need to fix up your route to tell Ember how to serialize your model.

App.CategoryRoute = Em.Route.extend({
  serialize: function(model){
    return {category_id: model.get('id')};
  }
});


来源:https://stackoverflow.com/questions/21838322/emberjs-target-a-model-without-leaving-models-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!