简体   繁体   中英

ember model function not executing when route is activated

I have an ember route that I've stripped down to

App.MyRoute = Ember.Route.extend({
    model: function(params){
        console.log("model function executing");
        Ember.Object.create()
    },
    setupController: function(controller){
        console.log("setupController function executed");
    }
});

When I switch to MyRoute, the setupController gets executed, but the function to populate the model never does. The model ends up just being the msg object that was passed in the {{link myRoute msg}} tag.

There are parts of the model that I need to load/compute at the time that we switch to that route. To do this I need to either be able to successfully update the model, or I need access to the params passed in the link from within the setupController function. Suggestions on how best to achieve this?

EDIT

To try to hash this out, I've created a complete minimal example that will produce this behavior:

my html is:

<html>
  <head>
    <title> This is my Page! </title> 

    <script src="js/libs/jquery-1.8.2.js"></script>
    <script src="js/libs/handlebars-1.0.rc.1.js"></script>
    <script src="js/libs/ember.js"></script>
    <script src="js/app.js"></script>
  </head>

  <body>
    <script type="text/x-handlebars">
      {{#linkTo example App.thing}}<p> go </p>{{/linkTo}}
      <div>
        {{outlet}}
      </div>
    </script>

    <script type="text/x-handlebars" data-template-name="index">
      <p> Initial Text </p>
    </script>

    <script type="text/x-handlebars" data-template-name="example">
      <p> After change </p>
    </script>
  </body>
</html>

with the app code:

var App = Ember.Application.create();

App.Router.map(function() {
    this.resource("example", {path: "/example/:id"});
});

App.thing = Ember.Object.create({
    id:10,
});

App.ExampleRoute = Ember.Route.extend({
    model: function(params){
        console.log("in model function");
        return new Ember.Object.create();
    },
    setupController: function(controller){
        console.log("in setupController");
    }
});

When you click on the link to the example route, "in setupController" prints, but "in model function" does not.

linkTo uses transitionTo behind the scenes. Whenever we use transitionTo , we actually provide the context/model directly and so the model method on the route is not called. In your example above, you have {{#linkTo example App.thing}} . Since we already known that the context is App.thing there's no reason to trigger the model method. We only call model on the route when we do not know what the model is. The main time this happens is when entering via a URL change.

It appears to work for me, and there's absolutely no reason why it shouldn't work: http://jsfiddle.net/SXTME/

Are you sure you're using EMBER 1.0.0-PRE.4, and not EMBER 1.0.0-PRE.3?

App.MyRoute = Ember.Route.extend({
    model: function(params){
        console.log("model function executing");
        Ember.Object.create()
    },
    setupController: function(controller){
        console.log("setupController function executed");
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM