Ember {{#link-to}}不会转换为路线

时间:2014-12-10 22:18:34

标签: ember.js routes ember-data

这是我目前的情况。我有一个广告系列视图路线,它嵌套在广告系列路线中。我使用动态细分,因此视图页面仅显示该特定广告系列的必要详细信息。

这在我的路线中有说明:

TM.Router.map(function(){
    this.resource("campaigns", function(){
        this.route("view", { path: "/view/:id" }),
        this.route("create"),
        this.route("edit", { path: "/edit/:id" })
    })
});

视图页面的URL为:

/campaigns/view/2

广告系列路线有一个编辑链接(仅显示在视图页面中),该链接应该将用户带到编辑页面,该表单预先填充了当前项目的详细信息。

所以我将模型链接到编辑页面:

TM.CampaignsEditRoute = Ember.Route.extend({
    model: function(params){
        return this.store.find("campaign", params.id);
    }
});

广告系列路线中的修改链接为:

  <li>{{#link-to "campaigns.edit"}}Edit Campaign Code Info{{/link-to}}</li>

链接更改为:

/campaigns/view/2

/campaigns/edit/2

..但由于某种原因,编辑模板不会在页面上显示/加载,也不会对模型发出GET请求,而且我不确定原因。

如果我刷新页面,则会显示表单,但初始点击时没有任何反应。

更新

当我将ID硬编码到{{#link-to}}时,

  <li>{{#link-to "campaigns.edit" "2"}}Edit Campaign Code Info{{/link-to}}</li>

该链接的工作原理应该如何,但是当我使用campaign.id时,我得到了

This link-to is in an inactive loading state because at least one of its parameters presently has a null/undefined value, or the provided route name is invalid.

更新2

问题在于,编辑广告系列的链接位于顶级路线(广告系列),而我要编辑的数据位于广告系列视图中。广告系列中的链接不知道广告系列视图中的数据ID是什么。

3 个答案:

答案 0 :(得分:2)

现在我有关于您的问题的更多信息 - 我决定发布另一个答案。问题是你有一个颜色模型,它不断改变你需要在父级别跟踪。

基于Ember.js&#39;文档here,我提出了以下解决方案:

App.ColorManagerController = Ember.Controller.extend({
  currentColour: null
});

App.IndexController = Ember.ArrayController.extend({
  needs: ['colorManager'],
  currentColour:Ember.computed.alias('controllers.colorManager.currentColour'),

  editMode: false,

  actions: {
    goToDetails: function(colour){
      this.set("editMode",true);
      return this.transitionToRoute("index.view", colour);
  }
 }
});

App.IndexViewController = Ember.ObjectController.extend({
  needs: ['colorManager'],

  currentColour:Ember.computed.alias('controllers.colorManager.currentColour'),

  modelChanged: function(){
    this.set('currentColour', this.get('model'));
  }.observes('model')

});

基本上,currentColour属性(虽然拼写错误;))在父控制器和子控制器之间同步。

然后,在您的index(父级)模板中,您只需要说

  {{#if editMode}}
    {{#link-to "index.edit" currentColour}}Edit Colour{{/link-to}}
  {{/if}}

jsbin上的工作解决方案here

答案 1 :(得分:1)

我认为应该是:

<li>{{#link-to "campaigns.edit" campaign}}Edit Campaign Code Info{{/link-to}}</li>

以下链接非常好地解释了这一点 - http://emberjs.com/guides/templates/links/

答案 2 :(得分:0)

我认为你应该对你的嵌套路由/资源结构进行更改。看一下“嵌套路由”部分下的此Ember页面,以获取示例: http://emberjs.com/guides/routing/defining-your-routes/

我会让我的路由器像:

TM.Router.map(function(){
    this.resource("campaigns", function(){
        this.route("create");
        this.resource("campaign", { path: "/:id", function(){
            this.route("view");
            this.route("edit");
        });
    })
});

你的链接帮助器现在应该隐含地使用url中的id param,这样链接就可以工作而不必向它传递值:

<li>{{#link-to "campaigns.edit"}}Edit Campaign Code Info{{/link-to}}</li>