【问题标题】:ember model function not executing when route is activated激活路由时未执行ember模型功能
【发布时间】:2013-01-13 11:39:27
【问题描述】:

我有一条 ember 路线,我已经精简到了

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

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

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

编辑

为了尝试解决这个问题,我创建了一个完整的最小示例来产生这种行为:

我的 html 是:

<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>

使用应用代码:

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");
    }
});

当您单击示例路由的链接时,会打印“in setupController”,但不会打印“in model function”。

【问题讨论】:

    标签: javascript ember.js


    【解决方案1】:

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

    【讨论】:

    • 谢谢,这或多或少是有道理的。回顾文档,我发现我还可以将 setupController 定义为 function(controller, model),这将解决我的其余用例。
    • 这不是说如果您知道某个网页的 URL,您就不必费心加载它,因为您必须已经知道那里有什么内容?知道一个 url 意味着您知道一些关于总页面“模型”的信息,可能(并且通常是)更多信息。
    【解决方案2】:

    它似乎对我有用,而且绝对没有理由不应该工作:http://jsfiddle.net/SXTME/

    您确定您使用的是 EMBER 1.0.0-PRE.4,而不是 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");
        }
    });
    

    【讨论】:

    • 正在运行 jquery-1.8.2.js handlebars-1.0.rc.1.js 和 ember-1.0.0-pre.4.js
    • 我有一个完整的应用程序,可以显示现在的行为。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    相关资源
    最近更新 更多