【问题标题】:Ember Transion-to URL not workingEmber 转换到 URL 不起作用
【发布时间】:2014-12-05 04:12:41
【问题描述】:

我有一个使用 jquery post 提交表单的 ember 操作,

 submit : function(){   //submitting new story
        this.set('onsubmit' , true);
        var self = this;
        $.post( "/api/post", { some post data })
            .done(function(data) {
               self.transitionTo('post' + data);
            });
        }

网址是这样的,域 example.com。帖子位于 example.com/api/post。发布后,用户必须重定向到 example.com/post/3432232(发布数据返回的一些值)。

但是,在将此转换发布到“example.com/api/post/5000203”而不是 example.com/post/234234234 之后。

哪个不存在并给出 404。如何使用 ember 中的转换功能让用户返回 example.com/post/234234234?

编辑 - 路线如下

App.Router.map(function() {    
this.resource('index', {path : '/'} , function() { 
    this.resource('p', function(){
        this.resource('post', { path: ':post_id' });
    });
});
});

谢谢

【问题讨论】:

    标签: javascript jquery ember.js


    【解决方案1】:

    在 Ember 路由系统中,transitionTo 将路由名称和路由模型作为参数(相同的模型,当调用 Ember.Route.model 挂钩时,该路由返回)。 Ember 的词汇表中有两种类型的路由器转换:URL 转换(发生在客户端处理新 URL 时,例如,当您从头开始打开页面时)和“命名转换”(当调用 this.tranisionTo 时发生)。

    Ember.Route 在处理“URL 转换”时尝试根据参数列表(例如:post_id)加载模型。另一方面,当它处理“命名转换”时,它不会调用模型钩子,而是使用已经提供的模型 (this.tranisition('post', PostModel)`)。 Docs.

    所以,如果您手头有 post_id,只需调用 this.store.find('post', postId) 来获取 PostModel 并将请求的模型提供给 PostRoute:this.transitionTo('post', this.store.find('post', data)

    附:请考虑阅读此问题和答案以使您的路由结构真正嵌套:Nested URLs: issues with route transition

    附言我查看了 Ember 的文档,发现 transitionTo 方法的新(至少对我而言)签名:http://emberjs.com/api/classes/Ember.Route.html#method_transitionTo。根据此文档,您可以重写您的示例以使其正常工作:this.tranisitionTo('post', data),其中data 是 post_id。

    【讨论】:

      猜你喜欢
      • 2015-10-18
      • 1970-01-01
      • 2013-04-28
      • 2014-12-01
      • 2016-01-30
      • 2020-04-03
      • 2015-09-17
      • 2017-08-16
      • 1970-01-01
      相关资源
      最近更新 更多