当路由被激活时,ember模型函数不执行 [英] ember model function not executing when route is activated

查看:134
本文介绍了当路由被激活时,ember模型函数不执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  App.MyRoute = Ember.Route.extend({ 
model:function(params){
console.log(model function execution);
Ember.Object.create()
},
setupController:function (控制器){
console.log(setupController function executed);
}
});

当我切换到MyRoute时,setupController被执行,但填充模型的功能永远不会。该模型最终只是在 {{link myRoute msg}} 标签中传递的 msg 对象。



在我们切换到该路由的时候,我需要加载/计算的部分模型。要做到这一点,我需要能够成功更新模型,或者我需要访问在setupController函数中链接中传递的参数。关于如何最好地实现这一点的建议?



编辑



为了尝试哈希,我创建了完整的最小例证将会产生这种行为:



我的HTML是:

 < HTML> 
< head>
< 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-handlebarsdata-template-name =index>
< p>初始文本< / p>
< / script>

< script type =text / x-handlebarsdata-template-name =example>
< p>改变之后< / p>
< / script>
< / body>
< / html>

与应用程序代码:

  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新的Ember.Object.create();
},
setupController:function(controller){
console.log(in setupController);
}
} );

当您点击链接到示例路由时,在setupController打印,但在模型功能没有。

解决方案

linkTo 使用 transitionTo 幕后。每当我们使用 transitionTo ,我们实际上直接提供了上下文/模型,因此路由上的模型方法不被调用。在上面的例子中,您有 {{#link to example App.thing}} 。因为我们已经知道上下文是 App.thing 没有理由触发模型方法。当我们不知道该模型是什么时,我们只会在路线上调用模型。发生这种情况的主要时间是通过URL更改输入。


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.

这篇关于当路由被激活时,ember模型函数不执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆