In latest Ember, how do you link to a route with just the id/name of a model, rather than providing all of its attributes in the linking page?

前端 未结 1 1501
别跟我提以往
别跟我提以往 2021-02-02 15:59

I ran into a problem when converting from Ember 1.0-pre2 to latest master (43354a98) and the new router, namely--

If I have a route which loads just the name and ID for

1条回答
  •  礼貌的吻别
    2021-02-02 16:25

    It seems like the model hook for App.BirdRoute never gets called when navigating using {{#linkTo}}... maybe this is because using {{#linkTo}} you are passing an object to the route from the birds route and ember doesn't think you should need to call model since it thinks the id has already been deserialized into an object. This seems to make sense since model gets called when you reload the page.

    I used the setupController hook to call your App.Bird.find() method and pass that result into the controller. This gets called by either a direct URL or through a {{#linkTo}} click. If called by the {{#linkTo}} helper link setupController the model parameter will be the bird object passed with the {{#linkTo}} helper. If called directly from the URL the returned value from the model hook will be passed into setupController as the model parameter.

    Here is a JSFiddle example

    example with accessible URL

    App.BirdRoute = Ember.Route.extend({
      model: function(params) {
        return {id: params.bird_id};
      },
      setupController: function(controller, model) {
        var bird_model = App.BirdTest.find(model.id);
        controller.set("content", bird_model);
      }
    });
    

    0 讨论(0)
提交回复
热议问题