knockout.jsrequirejsjquery-widgets

Can't use jqwidgets with knockout and requirejs


I'm gonna try again, I have been trying to find a solution to use jqwidgets in a single page app with knockout and requirejs.

My index.html :

<script src="app/require.config.js"></script>
<script data-main="app/startup" src="bower_modules/requirejs/require.js"/>

My require.config.js (mostly generated by yeoman) :

var require = {
baseUrl: ".",
paths: {
    "bootstrap":            "bower_modules/components-bootstrap/js/bootstrap.min",
    "crossroads":           "bower_modules/crossroads/dist/crossroads.min",
    "hasher":               "bower_modules/hasher/dist/js/hasher.min",
    "jquery":               "bower_modules/jquery/dist/jquery",
    "knockout":             "bower_modules/knockout/dist/knockout",
    "jqxknockout":          "bower_modules/jqwidgets/jqwidgets/jqxknockout",
    "jqx-all":              "bower_modules/jqwidgets/jqwidgets/jqx-all",
    "knockout-projections": "bower_modules/knockout-projections/dist/knockout-projections",
    "mapping":              "bower_modules/bower-knockout-mapping/dist/knockout.mapping",
    "signals":              "bower_modules/js-signals/dist/signals.min",
    "text":                 "bower_modules/requirejs-text/text"
},
shim: {
    "bootstrap": {export: "$", deps: ["jquery", "mapping", 'jqx-all'] }
}
};

My startup.js (called by index.html)

define(['jquery', 'knockout', './router', 'bootstrap', 'knockout-projections', 'jqx-all'], function ($, ko, router) {
ko.components.register('nav-bar', { require: 'components/nav-bar/nav-bar' });
ko.components.register('home-page', { require: 'components/home-page/home' });
ko.components.register('study-selection', { require: 'components/study-selection/study-selection' });
ko.applyBindings({ route: router.currentRoute });

});

It uses the router.js that also comes from yeoman:

return new Router({
    routes: [
        { url: '', params: { page: 'home-page' } },
        { url: 'study-selection', params: { page: 'study-selection' } }
    ]
});

function Router(config) {
    var currentRoute = this.currentRoute = ko.observable({});

    ko.utils.arrayForEach(config.routes, function(route) {
        crossroads.addRoute(route.url, function(requestParams) {
            currentRoute(ko.utils.extend(requestParams, route.params));
        });
    });

    activateCrossroads();
}

function activateCrossroads() {
    function parseHash(newHash, oldHash) { crossroads.parse(newHash); }
    crossroads.normalizeFn = crossroads.NORM_AS_OBJECT;
    hasher.initialized.add(parseHash);
    hasher.changed.add(parseHash);
    hasher.init();
}

});

Whenever I open the index page I can see that jqx-all is loaded. However when I try use the jquery widgets in any page, they are not rendered. study-selection.html :

<div id="jqxCheckBox" data-bind="jqxCheckBox: { checked: checked, disabled: disabled, width: '120px' }" style='margin-bottom: 10px;'>jqxCheckBox</div>

study-selection.js :

define(['knockout', 'text!./study-selection.html'], function (ko, templateMarkup) {


function Studyselection(params) {

  this.message = ko.observable('Hello from the studySelection1 component!');
  this.disabled = ko.observable(false);


  }
  return { viewModel: Studyselection, template: templateMarkup };

});

I have been looking through the examples on http://www.jqwidgets.com/jquery-widgets-documentation/documentation/requirejs/requirejs_tutorial_knockout.htm But I'm unable to find a solution to my problem. Is there way of debugging this? Am I missing something?

Any help is highly appreciated.

Kind Regards,


Solution

  • I finally figured it out. It seems that I had to put :

    $('#jqxcheckbox').jqxCheckBox({ width: 120, height: 25 }); 
    

    In order to render a checkbox in the studyselection.js file. Although it didn't show this in the examples shown on the jqwidgets demo's page.