2014-10-08 16 views
0

我想從我的請求「instance.web.Model」中得到結果,然後調用this.super()。問題是「instance.web.Model」是異步的,所以在我的情況下,super()將在請求完成之前被調用。如何在調用this._super之前趕上承諾? Javascript

MyObject.extend({ 

    init: function(parent, data){ 

     var newData = instance.web.Model('advanced.search') 
     .call('check_duplication', [data]).done(function (name) { 
      // do stuff 
      return name 
     }); 

     data = newData; 
     this._super.apply(this, arguments); 
     // super is called before my request is done so the new data are not sent to super. 

    } 
}); 

你知道如何解決這個問題嗎?爲了將newData數據作爲參數傳遞給超級對象。 PS:我試圖在自我封裝:var self = this;但它不起作用,因爲它似乎我擴展的父對象繼續運行而無需等待。所以,我有錯誤,如「self.super(...是不是一個函數」。

MyObject.extend({ 

    init: function(parent, data){ 
     var self = this; 

     var newData = instance.web.Model('advanced.search') 
     .call('check_duplication', [data]).done(function (name) { 
      // do stuff 
      var newData = name; 
      self._super.apply(self, parent, newData); 
      // or self._super.apply(self, arguments); etc... I tried many variantes 
     }); 

    } 
}); 

要回答BERGI,他問我什麼_super()呼籲該instance.web.Model叫蟒蛇腳本在服務器端,我想這是一種Ajax調用,但我測試許多情況下,我以爲這instance.web.Model調用是異步的所以這是我的對象擴展:。

instance.web.search.ExtendedSearchProposition = instance.web.Widget.extend(/** @lends instance.web.search.ExtendedSearchProposition# */{ 
template: 'SearchView.extended_search.proposition', 
events: { 
    'change .searchview_extended_prop_field': 'changed', 
    'change .searchview_extended_prop_op': 'operator_changed', 
    'click .searchview_extended_delete_prop': function (e) { 
     e.stopPropagation(); 
     this.getParent().remove_proposition(this); 
    } 
}, 
/** 
* @constructs instance.web.search.ExtendedSearchProposition 
* @extends instance.web.Widget 
* 
* @param parent 
* @param fields 
*/ 
init: function (parent, fields) { 
    this._super(parent); 
    this.fields = _(fields).chain() 
     .map(function(val, key) { return _.extend({}, val, {'name': key}); }) 
     .filter(function (field) { return !field.deprecated && (field.store === void 0 || field.store || field.fnct_search); }) 
     .sortBy(function(field) {return field.string;}) 
     .value(); 
    this.attrs = {_: _, fields: this.fields, selected: null}; 
    this.value = null; 
}, 

要進入進一步(希望它會幫助你)好吧,讓我們看看誰是超級類別:

instance.web.Widget = instance.web.Controller.extend({ 
// Backbone-ish API 
tagName: 'div', 
id: null, 
className: null, 
attributes: {}, 
events: {}, 
/** 
* The name of the QWeb template that will be used for rendering. Must be 
* redefined in subclasses or the default render() method can not be used. 
* 
* @type string 
*/ 
template: null, 
/** 
* Constructs the widget and sets its parent if a parent is given. 
* 
* @constructs instance.web.Widget 
* 
* @param {instance.web.Widget} parent Binds the current instance to the given Widget instance. 
* When that widget is destroyed by calling destroy(), the current instance will be 
* destroyed too. Can be null. 
*/ 
init: function(parent) { 
    this._super(parent); 
    // Bind on_/do_* methods to this 
    // We might remove this automatic binding in the future 
    for (var name in this) { 
     if(typeof(this[name]) == "function") { 
      if((/^on_|^do_/).test(name)) { 
       this[name] = this[name].bind(this); 
      } 
     } 
    } 
    // FIXME: this should not be 
    this.setElement(this._make_descriptive()); 
    this.session = instance.session; 
}, 

然後,在下一個:

instance.web.Controller = instance.web.Class.extend(instance.web.PropertiesMixin, { 
/** 
* Constructs the object and sets its parent if a parent is given. 
* 
* @param {instance.web.Controller} parent Binds the current instance to the given Controller instance. 
* When that controller is destroyed by calling destroy(), the current instance will be 
* destroyed too. Can be null. 
*/ 
init: function(parent) { 
    instance.web.PropertiesMixin.init.call(this); 
    this.setParent(parent); 
}, 

然後:

instance.web.PropertiesMixin = _.extend({}, instance.web.EventDispatcherMixin, { 
init: function() { 
    instance.web.EventDispatcherMixin.init.call(this); 
    this.__getterSetterInternalMap = {}; 
}, 

然後:

instance.web.EventDispatcherMixin = _.extend({}, instance.web.ParentedMixin, { 
__eventDispatcherMixin: true, 
init: function() { 
    instance.web.ParentedMixin.init.call(this); 
    this.__edispatcherEvents = new Events(); 
    this.__edispatcherRegisteredEvents = []; 
}, 

最後:

instance.web.ParentedMixin = { 
__parentedMixin : true, 
init: function() { 
    this.__parentedDestroyed = false; 
    this.__parentedChildren = []; 
    this.__parentedParent = null; 
}, 
+0

定義了'._super'在哪裏?如果它是由你的框架隱式創建的(你使用哪一個?),它很可能不會異步工作。 – Bergi 2014-10-08 09:42:04

+0

我注意到父母調用this_super(),也許這是我的情況的一個問題。我真的很抱歉沒有在我的信息中更加明確,我在這個領域的經驗並不是那麼大,所以我不知道從哪裏得到正確的信息來向你展示 – user3350239 2014-10-08 09:55:40

+0

我不知道'MyModel'是什麼,以及它有'延長'的方法?你可能使用Backbone? – Bergi 2014-10-08 09:57:27

回答

1

我不認爲動態super方法t大多數框架提供的異步工作 - 但如果你使用承諾,它確實需要(承諾永遠是異步的)。

所以,如果你需要調用自許回調父母的初始化方法,你可以嘗試

MyObject.extend({ 
    init: function(parent, data){ 
     var _super = this._super.bind(this); 

     instance.web.Model('advanced.search') 
     .call('check_duplication', [data]) 
     .done(function (name) { 
      _super(parent, name); 
     }); 
     // or in short, even just: 
     // .done(this._super.bind(this, parent)); 
    } 
}); 

或不使用_super,不過在native capability引用您的父類:

MyObject.extend({ 
    init: function(parent, data){ 
     var self = this; 

     instance.web.Model('advanced.search') 
     .call('check_duplication', [data]) 
     .done(function (name) { 
      instance.web.search.ExtendedSearchProposition.prototype.init // or whatever the parent method is 
       .call(self, parent, newData); 
     }); 
    } 
}); 
+0

我剛剛試過你的想法: Fisrt one A - > SyntaxError:super是保留標識符 第一個B - > TypeError:self.super不是函數 第二個 - > TypeError:dict.widget.attrs是未定義的+無限遞歸 將自己通過.done()作用域有問題 – user3350239 2014-10-08 10:15:16

+0

糟糕,第一個是我的愚蠢錯誤;第二個需要適應實際的超類(顯然'MyObject'不是超類) – Bergi 2014-10-08 10:20:41

+0

:) thx但仍然存在類似案例2 - > TypeError:dict.widget.attrs未定義的問題。怎麼樣jQuery BlockUI插件?我聽說它被用在我的程序中 – user3350239 2014-10-08 10:47:00

相關問題