骨干+承诺 - 如何发布后取 [英] Backbone + Promises - How to fetch after post

查看:156
本文介绍了骨干+承诺 - 如何发布后取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有两种功能的模型 - 一个岗位,一个得到

I have a model which has two functions - one posts, and one gets.

取指令:

fetch: function (options) {
  var self = this;

  return P1Comm.get({
    'dataType': 'json',
    'processAjaxResponse': self.processAjaxResponse,
    'onStatusInvalid': function (e) {
      P1.log(e, 'status');
    },
    'onSuccess': options.success,
    'onError': function (e) {
      P1.log(e);
    },
    'sourceURL': P1.API_APPS_ROOT + 'v1.0/accepted-terms'
  });
},

该职位:

acceptTerms: function (appId) {
  var self = this;

  self.set({
    'app_id': parseInt(appId,10)
  });
  self.createEdit(self.toJSON(), {
  }).pipe(function () {
    var def = $.Deferred();
    var saveOpts = {
        dataType: 'json',
        stringify: true,
        contentType: 'application/json',
        processData: false,
        processAjaxResponse: self.processAjaxResponse
      };
    self.save(self.toJSON(), saveOpts);
    return def.promise();
  });
},

从调用视图后: acceptAppTerms.acceptTerms(1);

这些都在他们自己的权利的工作,但我想,这样更改此:
- 使POST acceptTerms()
- 然后让GET 取()
- 然后调用另一个函数

These both work in their own right but I want to change this so that: - Make the POST acceptTerms() - Then make the GET fetch() - Then call another function

我认为这将是这样的(至少前两个分):

I think it would be something like this (at least for the first two points):

acceptAppTerms.acceptTerms(id).then(function () {
  this.launchApp(event, id);
});

不过,我得到未捕获类型错误的错误:无法读取属性,然后未定义

我并不擅长的前端 - ?有人可以点我在正确的方向

I am not that good at front-end - can someone point me in the right direction?

感谢

更新:

fetchAcceptedTerms: function () {
  var self = this;
  this.appAcceptedTerms = new T1AppAcceptedTerms();
  this.acceptedTerms = new AppAcceptedTerms();

  this.acceptedTerms.fetch({
    success: function (data) {
      if (data.meta.status === 'success') {
        self.appAcceptedTerms.set(data.data);
      }
    }
  });
},

下面的作品的例子,但它打破了数据进入模型的设定。

Example below works but it breaks the setting of the data into the model.

推荐答案

你的错误是因为你的 acceptTerms 不返回任何东西,或者换句话说原因返回未定义不具有则()方法。

The reason you get the error is because your acceptTerms doesn't return anything, or in other words returns undefined which doesn't have a then() method.

您code应该是这样的:

Your code should be something like:

fetch: function(options) {
    var self = this;
    return P1Comm.get({
      dataType: 'json',
      processAjaxResponse: self.processAjaxResponse,
      onStatusInvalid: function(e) {
        P1.log(e, 'status');
      },
      onSuccess: options.success,
      onError: function(e) {
        P1.log(e);
      },
      sourceURL: P1.API_APPS_ROOT + 'v1.0/accepted-terms'
    });
},

acceptTerms: function(appId) {
    var self = this;
    self.set({
      'app_id': parseInt(appId, 10)
    });

    return self.createEdit(self.toJSON(), {}).pipe(function() {
    //---------------------------------------^ better use .then()
      var saveOpts = {
        dataType: 'json',
        stringify: true,
        contentType: 'application/json',
        processData: false,
        processAjaxResponse: self.processAjaxResponse
      };
      return self.save(self.toJSON(), saveOpts);
    });
},

这code asummes的 P1Comm.get self.createEdit self.save 返回一个承诺对象。如果他们不这样做,那么你需要创建一个递延对象并返回它的这些功能​​的承诺。而成功的内部/异步自己的行为错误回调需要决心/拒绝的相应承诺。

This code asummes that P1Comm.get, self.createEdit and self.save returns a promise object. If they don't then you need to create a Deferred object and return it's promise from these functions. And inside the success/error callbacks of their asynchronous actions you need to resolve/reject the promise accordingly.

我也想提一提, .pipe() 是一个去precated方法,你应该使用 则() 除非你正在使用jQuery的古版本。

I'd also like to mention that .pipe() is a deprecated method, you should use then() unless you're using ancient version of jQuery.

这篇关于骨干+承诺 - 如何发布后取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆