我想从我的请求“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;
},
定义了'._super'在哪里?如果它是由你的框架隐式创建的(你使用哪一个?),它很可能不会异步工作。 – Bergi 2014-10-08 09:42:04
我注意到父母调用this_super(),也许这是我的情况的一个问题。我真的很抱歉没有在我的信息中更加明确,我在这个领域的经验并不是那么大,所以我不知道从哪里得到正确的信息来向你展示 – user3350239 2014-10-08 09:55:40
我不知道'MyModel'是什么,以及它有'延长'的方法?你可能使用Backbone? – Bergi 2014-10-08 09:57:27