Commit e5140d38 authored by Romain Courteaud's avatar Romain Courteaud

Implement declareMethod.

Use it to simplify the implementation of the default accessor.
parent 72914726
...@@ -28,91 +28,59 @@ ...@@ -28,91 +28,59 @@
RenderJSGadget.prototype.required_css_list = []; RenderJSGadget.prototype.required_css_list = [];
RenderJSGadget.prototype.required_js_list = []; RenderJSGadget.prototype.required_js_list = [];
// Returns the list of gadget prototype RenderJSGadget.prototype.declareMethod = function (name, callback) {
RenderJSGadget.prototype.getInterfaceList = function () { // // Register the potentially loading javascript
var dfr = $.Deferred(), // var script_element = $('script').last(),
gadget = this; // src = script_element.attr('src');
setTimeout(function () { // if (src !== undefined) {
dfr.resolve(gadget.interface_list); // if (javascript_registration_dict[src] === undefined) {
}); // // First time loading the JS file.
return dfr.promise(); // // Remember all declareMethod calls
}; // javascript_registration_dict[src] = {
// loaded: false,
// Returns a list of CSS required by the gadget // method_list: [[name, callback]],
RenderJSGadget.prototype.getRequiredCSSList = function () { // };
var dfr = $.Deferred(), // script_element.load(function () {
gadget = this; // javascript_registration_dict[src].loaded = true;
setTimeout(function () { // });
dfr.resolve(gadget.required_css_list); // } else if (!javascript_registration_dict[src].loaded) {
}); // javascript_registration_dict[src].method_list.push([name, callback]);
return dfr.promise(); // }
}; // }
// Returns a list of JS required by the gadget
RenderJSGadget.prototype.getRequiredJSList = function () {
var dfr = $.Deferred(),
gadget = this;
setTimeout(function () {
dfr.resolve(gadget.required_js_list);
});
return dfr.promise();
};
// Returns the path of the code of a gadget
RenderJSGadget.prototype.getPath = function () {
var dfr = $.Deferred(),
gadget = this;
setTimeout(function () {
dfr.resolve(gadget.path);
});
return dfr.promise();
};
// Returns the title of a gadget this.constructor.prototype[name] = function () {
RenderJSGadget.prototype.getTitle = function () { return $.when(callback.apply(this, arguments));
var dfr = $.Deferred(), };
gadget = this; // Allow chain
setTimeout(function () { return this;
dfr.resolve(gadget.title);
});
return dfr.promise();
}; };
// Returns the HTML of a gadget RenderJSGadget.prototype
RenderJSGadget.prototype.getHTML = function () { .declareMethod('getInterfaceList', function () {
var dfr = $.Deferred(), // Returns the list of gadget prototype
gadget = this; return this.interface_list;
setTimeout(function () { })
dfr.resolve(gadget.html); .declareMethod('getRequiredCSSList', function () {
// Returns a list of CSS required by the gadget
return this.required_css_list;
})
.declareMethod('getRequiredJSList', function () {
// Returns a list of JS required by the gadget
return this.required_js_list;
})
.declareMethod('getPath', function () {
// Returns the path of the code of a gadget
return this.path;
})
.declareMethod('getTitle', function () {
// Returns the title of a gadget
return this.title;
})
.declareMethod('getHTML', function () {
// Returns the HTML of a gadget
return this.html;
}); });
return dfr.promise();
};
// RenderJSGadget.prototype.declareMethod = function (name, callback) {
// // Register the potentially loading javascript
// var script_element = $('script').last(),
// src = script_element.attr('src');
// if (src !== undefined) {
// if (javascript_registration_dict[src] === undefined) {
// // First time loading the JS file.
// // Remember all declareMethod calls
// javascript_registration_dict[src] = {
// loaded: false,
// method_list: [[name, callback]],
// };
// script_element.load(function () {
// javascript_registration_dict[src].loaded = true;
// });
// } else if (!javascript_registration_dict[src].loaded) {
// javascript_registration_dict[src].method_list.push([name, callback]);
// }
// }
//
// // Add the method on the gadget prototype
// RenderJSGadget.prototype[name] = callback;
// return this;
// };
//
// $.parseGadgetHTML = function (data) { // $.parseGadgetHTML = function (data) {
// // var xml = $.parseXML(data); // // var xml = $.parseXML(data);
// // var xml = $(data); // // var xml = $(data);
......
...@@ -937,4 +937,116 @@ ...@@ -937,4 +937,116 @@
}); });
}); });
/////////////////////////////////////////////////////////////////
// RenderJSGadget.declareMethod
/////////////////////////////////////////////////////////////////
module("RenderJSGadget.declareMethod");
test('is chainable', function () {
// Check that declareMethod is chainable
// Subclass RenderJSGadget to not pollute its namespace
var Klass = function () {
RenderJSGadget.call(this);
}, gadget, result;
Klass.prototype = new RenderJSGadget();
Klass.prototype.constructor = Klass;
gadget = new Klass();
equal(gadget.testFoo, undefined);
result = gadget.declareMethod('testFoo', function () {
var a;
});
// declareMethod is chainable
equal(result, gadget);
});
test('creates methods on the prototype', function () {
// Check that declareMethod create a callable on the prototype
// Subclass RenderJSGadget to not pollute its namespace
var Klass = function () {
RenderJSGadget.call(this);
}, gadget, called, result;
Klass.prototype = new RenderJSGadget();
Klass.prototype.constructor = Klass;
gadget = new Klass();
equal(gadget.testFoo, undefined);
gadget.declareMethod('testFoo', function (value) {
called = value;
});
// Method is added on the instance class prototype
equal(RenderJSGadget.prototype.testFoo, undefined);
ok(gadget.testFoo !== undefined);
ok(Klass.prototype.testFoo !== undefined);
equal(Klass.prototype.testFoo, gadget.testFoo);
// method can be called
gadget.testFoo("Bar");
equal(called, "Bar");
});
test('returns a promise when synchronous function', function () {
// Check that declareMethod returns a promise when defining
// a synchronous function
// Subclass RenderJSGadget to not pollute its namespace
var Klass = function () {
RenderJSGadget.call(this);
}, gadget;
Klass.prototype = new RenderJSGadget();
Klass.prototype.constructor = Klass;
gadget = new Klass();
gadget.declareMethod('testFoo', function (value) {
return value;
});
// method can be called
stop();
gadget.testFoo("Bar")
.done(function (param) {
equal(param, "Bar");
})
.fail(function () {
ok(false, "Should not fail when synchronous");
})
.always(function () {
start();
});
});
test('returns the callback promise if it exists', function () {
// Check that declareMethod returns the promise created by the callback
// Subclass RenderJSGadget to not pollute its namespace
var Klass = function () {
RenderJSGadget.call(this);
}, gadget;
Klass.prototype = new RenderJSGadget();
Klass.prototype.constructor = Klass;
gadget = new Klass();
gadget.declareMethod('testFoo', function (value) {
var dfr = $.Deferred();
setTimeout(function () {
dfr.reject(value);
});
return dfr.promise();
});
// method can be called
stop();
gadget.testFoo("Bar")
.done(function () {
ok(false, "Callback promise is rejected");
})
.fail(function (param) {
equal(param, "Bar");
})
.always(function () {
start();
});
});
}(document, jQuery, renderJS, QUnit, sinon)); }(document, jQuery, renderJS, QUnit, sinon));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment