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,67 +28,7 @@ ...@@ -28,67 +28,7 @@
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 () {
var dfr = $.Deferred(),
gadget = this;
setTimeout(function () {
dfr.resolve(gadget.interface_list);
});
return dfr.promise();
};
// Returns a list of CSS required by the gadget
RenderJSGadget.prototype.getRequiredCSSList = function () {
var dfr = $.Deferred(),
gadget = this;
setTimeout(function () {
dfr.resolve(gadget.required_css_list);
});
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
RenderJSGadget.prototype.getTitle = function () {
var dfr = $.Deferred(),
gadget = this;
setTimeout(function () {
dfr.resolve(gadget.title);
});
return dfr.promise();
};
// Returns the HTML of a gadget
RenderJSGadget.prototype.getHTML = function () {
var dfr = $.Deferred(),
gadget = this;
setTimeout(function () {
dfr.resolve(gadget.html);
});
return dfr.promise();
};
// RenderJSGadget.prototype.declareMethod = function (name, callback) {
// // Register the potentially loading javascript // // Register the potentially loading javascript
// var script_element = $('script').last(), // var script_element = $('script').last(),
// src = script_element.attr('src'); // src = script_element.attr('src');
...@@ -107,12 +47,40 @@ ...@@ -107,12 +47,40 @@
// javascript_registration_dict[src].method_list.push([name, callback]); // javascript_registration_dict[src].method_list.push([name, callback]);
// } // }
// } // }
//
// // Add the method on the gadget prototype this.constructor.prototype[name] = function () {
// RenderJSGadget.prototype[name] = callback; return $.when(callback.apply(this, arguments));
// return this; };
// }; // Allow chain
// return this;
};
RenderJSGadget.prototype
.declareMethod('getInterfaceList', function () {
// Returns the list of gadget prototype
return this.interface_list;
})
.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;
});
// $.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