Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
R
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
Xiaowu Zhang
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 @@
...
@@ -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);
...
...
test/renderjs_test2.js
View file @
e5140d38
...
@@ -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
));
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