Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
renderjs
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gabriel Monnerat
renderjs
Commits
e5140d38
Commit
e5140d38
authored
Jul 09, 2013
by
Romain Courteaud
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement declareMethod.
Use it to simplify the implementation of the default accessor.
parent
72914726
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
160 additions
and
80 deletions
+160
-80
renderjs.js
renderjs.js
+48
-80
test/renderjs_test2.js
test/renderjs_test2.js
+112
-0
No files found.
renderjs.js
View file @
e5140d38
...
...
@@ -28,91 +28,59 @@
RenderJSGadget
.
prototype
.
required_css_list
=
[];
RenderJSGadget
.
prototype
.
required_js_list
=
[];
// Returns the list of gadget prototype
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
();
};
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]);
// }
// }
// Returns the title of a gadget
RenderJSGadget
.
prototype
.
getTitle
=
function
()
{
var
dfr
=
$
.
Deferred
(),
gadget
=
this
;
setTimeout
(
function
()
{
dfr
.
resolve
(
gadget
.
title
);
});
return
dfr
.
promise
();
this
.
constructor
.
prototype
[
name
]
=
function
()
{
return
$
.
when
(
callback
.
apply
(
this
,
arguments
));
};
// Allow chain
return
this
;
};
// Returns the HTML of a gadget
RenderJSGadget
.
prototype
.
getHTML
=
function
()
{
var
dfr
=
$
.
Deferred
(),
gadget
=
this
;
setTimeout
(
function
()
{
dfr
.
resolve
(
gadget
.
html
);
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
;
});
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) {
// // var xml = $.parseXML(data);
// // var xml = $(data);
...
...
test/renderjs_test2.js
View file @
e5140d38
...
...
@@ -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
));
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment