5

I have recently started to work with requirejs and when I try to create a simple viewmodel I get an strange exception. The exception comes from the knockout-2.1.0.js file and the exception is "Only subscribable things can act as dependencies".

define("PageViewModel", ["knockout-2.1.0"], function(ko) {
    return function PageViewModel() {
        var self = this;
        self.visiblePage = ko.observable("StartPage");
        self.showPage = function (pageName) {
            self.visiblePage(pageName);
        };
    };
});

As you can see the viewmodel is extremly simple and since the error is in the knockout js file, it seems like requirejs is working as it should. I have been looking at: http://knockoutjs.com/documentation/amd-loading.html The exception occur when coming to the line: self.visiblePage = ko.observable("StartPage");

Any ideas on what I'm doing wrong?

Thanks, Ludwig

Update: This is the module containing the pageviewmodel:

define("ViewModelFactory", ["StorageService", "PageViewModel", "AddUnitViewModel", "AddRoomViewModel"],
function (StorageService, PageViewModel, AddUnitViewModel, AddRoomViewModel) {
    //var repositoryStorage = new StorageService();
    var createAddRoomVM = function () {
        var vm = new AddRoomViewModel();
        vm.setRepository = StorageService.getRoomRepository();
        return vm;
    };
    var createAddUnitVM = function () {
        var vm = new AddUnitViewModel();
        vm.setRepository = StorageService.getUnitRepository();
        return vm;
    };
    var createPageVM = function () {
        var vm = new PageViewModel();
        return vm;
    };

    return {
        createPageVM:createPageVM,
        createAddRoomVM: createAddRoomVM,
        createAddUnitVM: createAddUnitVM
    };
});

And the module calling the factory

define("ApplicationViewModel", ["ViewModelFactory"],
function (viewModelFactory) {
    mainVM = null;
    var initVM = function () {
        mainVM = {
            page: viewModelFactory.createPageVM(),
            addRoom: viewModelFactory.createAddRoomVM(),
            addUnit: viewModelFactory.createAddUnitVM()
        };
    };

    var getVM = function (viewName) {
        switch (viewName) {
            case "AddRoom":
                return mainVM.addRoom;
            case "AddUnit":
                return mainVM.addUnit;
            default:
                return null;
        }
    };
    var getPageVM = function () {
        return mainVM.page;
    };

    return {
        initVM: initVM,
        getVM: getVM,
        getPageVM: getPageVM,
        mainVM: mainVM
    };
});

And the class containing the applicationViewModel:

define("Bootstrapper", ["knockout-2.1.0", "Routing", "ApplicationViewModel"],
function (ko, routing, applicationViewModel) {
    var run = function () {
        applicationViewModel.initVM(); <-- after here mainVM.page is null
        var mainVM = applicationViewModel.mainVM;
        routing.initRouting(applicationViewModel);
        ko.applyBindings(mainVM);
        routing.showView("StartPage");
        alert("Start");
    };

    return {
        run: run
    };
})
7
  • Can you also post your code where you use your PageViewModel and maybe also your view code?
    – nemesv
    Nov 21, 2012 at 16:04
  • according to ko source code, in line 956, that exception occurs when property is not subscriable.
    – Ray
    Nov 22, 2012 at 0:30
  • The property should be subscribable as it's just a regular variable I suppose... Nov 22, 2012 at 13:23
  • Do you use minified or debug version of knockout?
    – xdenser
    Nov 22, 2012 at 23:42
  • 2
    I updated knockout to 2.2.0 and then changed to debug it suddenly started to work! Nov 24, 2012 at 21:30

1 Answer 1

1

Your problem may have been caused by Knockout 2.1, which didn't work well when ko was not a global variable.

Knockout 2.2 should work fine, and I see from your comment this did indeed fix the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.