Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knockout viewmodel and requirejs

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
    };
})
like image 950
user1842278 Avatar asked Nov 13 '22 17:11

user1842278


1 Answers

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.

like image 197
Judah Gabriel Himango Avatar answered Dec 16 '22 05:12

Judah Gabriel Himango