load knockout grid binding as a require js module

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1305660 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2012-03-26 23:05:09 +00:00
parent 4595009682
commit 2a276fd69d
4 changed files with 65 additions and 59 deletions

View File

@ -85,8 +85,8 @@
"choosen": "chosen.jquery-0.9.7",
"jquery_validate": "jquery.validate-1.9.0",
"jquery_json": "jquery.json-2.3.min",
"knockout.simpleGrid": "knockout.simpleGrid",
"knockout": "knockout-2.0.0.debug",
"knockout.simpleGrid": "knockout.simpleGrid",
"knockout.sortable": "knockout-sortable",
"redback": "redback/redback",
"general-admin":"archiva/general-admin",

View File

@ -18,7 +18,8 @@
*/
define("main",["order!jquery","jquery_ui","jquery_cookie","bootstrap","order!archiva/search",
"jquery_validate","jquery_json","order!knockout","order!redback-templates","order!main-templates","order!roles",
"order!redback","general-admin","repositories","network-proxies","proxy-connectors","repository-groups"],
"order!redback","general-admin","repositories","network-proxies","proxy-connectors","repository-groups",
"order!knockout.simpleGrid"],
function() {
/**

View File

@ -9,64 +9,68 @@
// * Creating some API to fetch table data using Ajax requests
// ... etc
(function () {
define("knockout.simpleGrid",["jquery","order!utils","i18n","order!knockout"], function() {
(function () {
ko.simpleGrid = {
// Defines a view model class you can use to populate a grid
viewModel: function (configuration) {
this.data = configuration.data;
this.currentPageIndex = ko.observable(0);
this.pageSize = configuration.pageSize || 5;
this.columns = configuration.columns;
ko.simpleGrid = {
// Defines a view model class you can use to populate a grid
viewModel: function (configuration) {
this.data = configuration.data;
this.currentPageIndex = ko.observable(0);
this.pageSize = configuration.pageSize || 5;
this.columns = configuration.columns;
this.itemsOnCurrentPage = ko.computed(function () {
var startIndex = this.pageSize * this.currentPageIndex();
return this.data.slice(startIndex, startIndex + this.pageSize);
}, this);
this.itemsOnCurrentPage = ko.computed(function () {
var startIndex = this.pageSize * this.currentPageIndex();
return this.data.slice(startIndex, startIndex + this.pageSize);
}, this);
this.maxPageIndex = ko.computed(function () {
return Math.ceil(ko.utils.unwrapObservable(this.data).length / this.pageSize);
}, this);
this.i18n=function(key){
return $.i18n.prop(key);
};
this.gridUpdateCallBack = configuration.gridUpdateCallBack;
this.pageLinksUpdateCallBack = configuration.pageLinksUpdateCallBack;
}
};
// Templates used to render the grid
var templateEngine = new ko.jqueryTmplTemplateEngine();
// The "simpleGrid" binding
ko.bindingHandlers.simpleGrid = {
// This method is called to initialize the node, and will also be called again if you change what the grid is bound to
update: function (element, viewModelAccessor, allBindingsAccessor) {
var viewModel = viewModelAccessor(), allBindings = allBindingsAccessor();
// Empty the element
while(element.firstChild) {
ko.removeNode(element.firstChild);
}
// Allow the default templates to be overridden
var gridTemplateName = allBindings.simpleGridTemplate || "ko_usersGrid_grid",
pageLinksTemplateName = allBindings.simpleGridPagerTemplate || "ko_simpleGrid_pageLinks";
// Render the main grid
var gridContainer = element.appendChild(document.createElement("DIV"));
ko.renderTemplate(gridTemplateName, viewModel, { templateEngine: templateEngine }, gridContainer, "replaceNode")
.subscribe(viewModel.gridUpdateCallBack?viewModel.gridUpdateCallBack:function(){});
if (viewModel.gridUpdateCallBack) viewModel.gridUpdateCallBack();
// Render the page links
var pageLinksContainer = $("#"+allBindings.pageLinksId).get(0);
ko.renderTemplate(pageLinksTemplateName, viewModel, { templateEngine: templateEngine }, pageLinksContainer, "replaceNode")
.subscribe(viewModel.pageLinksUpdateCallBack?viewModel.pageLinksUpdateCallBack:function(){});
if (viewModel.pageLinksUpdateCallBack) viewModel.pageLinksUpdateCallBack();
this.maxPageIndex = ko.computed(function () {
return Math.ceil(ko.utils.unwrapObservable(this.data).length / this.pageSize);
}, this);
this.i18n=function(key){
return $.i18n.prop(key);
};
this.gridUpdateCallBack = configuration.gridUpdateCallBack;
this.pageLinksUpdateCallBack = configuration.pageLinksUpdateCallBack;
}
};
})();
};
// Templates used to render the grid
var templateEngine = new ko.jqueryTmplTemplateEngine();
// The "simpleGrid" binding
ko.bindingHandlers.simpleGrid = {
// This method is called to initialize the node, and will also be called again if you change what the grid is bound to
update: function (element, viewModelAccessor, allBindingsAccessor) {
var viewModel = viewModelAccessor(), allBindings = allBindingsAccessor();
// Empty the element
while(element.firstChild) {
ko.removeNode(element.firstChild);
}
// Allow the default templates to be overridden
var gridTemplateName = allBindings.simpleGridTemplate || "ko_usersGrid_grid",
pageLinksTemplateName = allBindings.simpleGridPagerTemplate || "ko_simpleGrid_pageLinks";
// Render the main grid
var gridContainer = element.appendChild(document.createElement("DIV"));
ko.renderTemplate(gridTemplateName, viewModel, { templateEngine: templateEngine }, gridContainer, "replaceNode")
.subscribe(viewModel.gridUpdateCallBack?viewModel.gridUpdateCallBack:function(){});
if (viewModel.gridUpdateCallBack) viewModel.gridUpdateCallBack();
// Render the page links
var pageLinksContainer = $("#"+allBindings.pageLinksId).get(0);
ko.renderTemplate(pageLinksTemplateName, viewModel, { templateEngine: templateEngine }, pageLinksContainer, "replaceNode")
.subscribe(viewModel.pageLinksUpdateCallBack?viewModel.pageLinksUpdateCallBack:function(){});
if (viewModel.pageLinksUpdateCallBack) viewModel.pageLinksUpdateCallBack();
}
};
})();
})

View File

@ -16,7 +16,8 @@
* specific language governing permissions and limitations
* under the License.
*/
define("redback",["jquery","order!utils","jquery_validate","jquery_json","roles","user","users"], function() {
define("redback",["jquery","order!utils","jquery_validate","jquery_json","order!knockout",
"order!knockout.simpleGrid","roles","user","users"], function() {
// define a container object with various datas
window.redbackModel = {userOperationNames:null,key:null,i18n:$.i18n.map};