Backbone 和 Express:在 res.redirect 上连接(复制)路线

Backbone and Express: concatinating (duplicating) routes on res.redirect

我有一项操作需要更新 MongoDB 条目,包括 _id 字段,这需要删除旧条目并创建一个新条目,这里是服务器端:

exports.update = function(req, res, next){
  var outcome = [];

  outcome.previousId = req.params.id;
  outcome.newId = req.body.name;

  var getPreviousRecord = function(callback) {
    req.app.db.models.AccountGroup
      .findOne({ _id: req.params.id })
      .lean()
      .exec(function(err, accountGroups) {
        if (err) {
          return callback(err, null);
        }

        outcome.accountGroups = accountGroups;
        return callback(null, 'done');
      });
  };

  var makeNewRecord = function(callback) {
    var permissions = outcome.accountGroups.permissions;
    var fieldsToSet = {
      _id: outcome.newId.toLowerCase(),
      name: outcome.newId,
      permissions: permissions
    };

    req.app.db.models.AccountGroup
      .create(fieldsToSet, function(err, record) {
        if (err) {
          return callback(err, null);
        }

        outcome.record = record;
        return callback(null, 'done');
      });
  };

  var deletePreviousRecord = function() {
    req.app.db.models.AccountGroup
      .findByIdAndRemove(outcome.previousId)
      .exec(function(err) {
        if (err) {
          return next(err);
        }

        res.redirect('admin/account-groups/' + outcome.newId + '/');
      });
  };

  var asyncFinally = function(err) {
    if (err) {
      return next(err);
    }
  };

  require('async').series([getPreviousRecord, makeNewRecord, deletePreviousRecord], asyncFinally);
};

它工作正常,但我不能让它在前端正常工作,它returns我既是旧路由又是新路由,例如:

PUT /admin/account-groups/customers22/admin/account-groups/Customers2233/ 404 213.749 ms - 31

其中 customers22 是旧的 _id,customers2233 是新的 _id。如果我从另一个页面导航到新条目,它会正常获取路由。

在客户端:

(function() {
  'use strict';

  app = app || {};

  app.Details = Backbone.Model.extend({
    idAttribute: '_id',
    defaults: {
      success: false,
      errors: [],
      errfor: {},
      name: ''
    },
    url: function() {
      return '/admin/account-groups/'+ app.mainView.model.id +'/';
    },
    parse: function(response) {
      if (response.accountGroup) {
        app.mainView.model.set(response.accountGroup);
        delete response.accountGroup;
      }

      return response;
    }
  });

  app.DetailsView = Backbone.View.extend({
    el: '#details',
    events: {
      'click .btn-update': 'update'
    },
    template: Handlebars.compile( $('#tmpl-details').html() ),
    initialize: function() {
      this.model = new app.Details();
      this.syncUp();
      this.listenTo(app.mainView.model, 'change', this.syncUp);
      this.listenTo(this.model, 'sync', this.render);
      this.render();
    },
    syncUp: function() {
      this.model.set({
        _id: app.mainView.model.id,
        name: app.mainView.model.get('name')
      });
    },
    render: function() {
      this.$el.html(this.template( this.model.attributes ));

      for (var key in this.model.attributes) {
        if (this.model.attributes.hasOwnProperty(key)) {
          this.$el.find('[name="'+ key +'"]').val(this.model.attributes[key]);
        }
      }
    },
    update: function() {
      this.model.save({
        name: this.$el.find('[name="name"]').val()
      });
    }

  });

  app.MainView = Backbone.View.extend({
    el: '.page .container',
    initialize: function() {
      app.mainView = this;
      this.model = new app.AccountGroup( JSON.parse( unescape($('#data-record').html()) ) );

      // ...
      app.detailsView = new app.DetailsView();
    }
  });

  $(document).ready(function() {
    app.mainView = new app.MainView();
  });   
}());

可能需要同时触发 model.savemodel.destroy 或阻止使用 URL。感谢您提供有关如何操作的任何建议,谢谢。

编辑 只是这里的错别字,与题目无关,胡乱查路线,看as cancelled

我认为问题出在这里:

res.redirect('admin/account-groups/' + outcome.newId + '/');

这是一个相对路径,因此它将附加到当前 URL。我怀疑你想要这样的东西:

res.redirect('/admin/account-groups/' + outcome.newId + '/');