Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js 修改模型后更新URL_Ember.js_Ember Data - Fatal编程技术网

Ember.js 修改模型后更新URL

Ember.js 修改模型后更新URL,ember.js,ember-data,Ember.js,Ember Data,我有一个类似于“loans/:loan_id”的路由,从一个链接重定向到这个URL,并发送一个未保存的loan对象作为模型,因此id为null,这导致URL为“loans/null”,但是我保存了模型,它从服务器获得了一个id,但是我如何更新URL,使其显示新的id而不是null 谢谢。我想你有一个LoanNewController或类似的东西。然后,我进一步假设您在创建新创建的loan对象后,正试图直接转换到该对象,这将显示一个null id,因为创建loan操作是异步的,您必须等待在后端创建

我有一个类似于“loans/:loan_id”的路由,从一个链接重定向到这个URL,并发送一个未保存的loan对象作为模型,因此id为null,这导致URL为“loans/null”,但是我保存了模型,它从服务器获得了一个id,但是我如何更新URL,使其显示新的id而不是null


谢谢。

我想你有一个
LoanNewController
或类似的东西。然后,我进一步假设您在创建新创建的
loan
对象后,正试图直接转换到该对象,这将显示一个null id,因为创建
loan
操作是异步的,您必须等待在后端创建贷款,然后再进行转换,因此,为了使其工作,您可以执行以下操作:

App.LoanNewController = Ember.ObjectController.extend({
  saveLoan: function() {
    ...
    this.get('store').commit();
  },
  ...
  transitionAfterSave: function() {
    if(this.get('content.id')) {
      this.transitionToRoute('loan', this.get('content'));
    }
  }.observes(content.id)
添加的观察者将观察
content.id
,当设置它时(当服务器调用返回时),将调用
transitionAfterSave
,并且转换将使用正确的id将内容传递给它

这个答案主要基于假设,因为你没有透露那么多代码,但我想你明白了


希望有帮助。

谢谢!但这会在浏览器历史记录中创建条目吗?我以前尝试过使用loan.one('didCreate')事件,但当我在浏览器中按回时,会返回空URL,这导致应用程序中出现一些意外行为。