Knockoutjs saves multiple view models from a single function?

I have spent the last few days researching the following KnockoutJS issue.

I have a page with 3 viewmodels on. I am using div’s with id’s to specify the bindings i.e:

ko.applyBindings(new viewModel(datasource), $("#sectionQualifications")[0]);

I am also using RequireJS which has really helped with making my app modular and works well with KnockoutJS of course.

My question relates to having (as mentioned) 3 viewmodels on my page.. none overlap, but each viewmodel has a SAVE function. So a quick look at one of the viewmodel snippets:

function viewModel(data) {
        self = this;

        self.quals = ko.observableArray(data),

        self.addQual = function () {
            self.quals.push(new qualification());

        self.remove = function (item) {

            //  Remove from the database IF we have an actual record in our viewmodel
            if (item.Id !== 0) {
                dataservice_qualifications.deleteEntity(item.Id, ko.toJSON(item),
                        success: function (ret) {
                            common.notifyOK('Qualification removed');
                        error: function (err) {
                            common.notifyError('Cannot remove that qualification');
                            console.log('Qualification Remove Error', err);
                            console.log('Remove error object', this.Id);

            //  Remove from the actual view model

        //  Save and move on.. we need to iterate through the qualifications, update any existing rows (ID NOT 0) or
        //  add new entries (ID IS 0) = function () {

            var saveData = ko.toJS(this.quals);
            for (var i in saveData) {

                //  New qualification entry
                if (saveData[i].Id === 0) { // New qualification entry
                            success: function (ret) {
                            error: function (error) {
                                common.notifyError('Cannot add qualification ' + saveData[i].qualificationName);
                                console.log('Qualification add error', error);
                } // eof NEW qualification

                if (saveData[i].Id > 0) {
                    dataservice_qualifications.putEntity(saveData[i].Id, ko.toJSON(saveData[i]),
                            success: function (ret) {
                            error: function (error) {
                                common.notifyError('Cannot update qualification ' + saveData[i].qualificationName);
                                console.log('UPDATED: ERROR:', error);
                } // eof UPDATED qualification

            } // eof saveData loop

            common.notifyOK('Qualifications updated');

        } // eof savenext function


So from that above sample, I would have 2 other viewmodels that are similar, which have the SAVE function as above. So of course I want to use say jQuery to click a button and save all 3 viewmodels (i.e. via that SAVE function on each).

Because I am using RequireJS, I have tried exposing a “public” function that in turn tries to internally call the function as follows in this snippet:

function saveModel() {;


return {
    saveModel: saveModel,
    loadViewModel: koMapData

So the theory being I can call the “saveModel” function from wherever which should trigger the save of the viewmodels?

Any help really appreciated. By the way I have gone down the path of trying to create the viewmodel like:

var viewModel = {

    save: function() {
        blah blah...

however no real luck in that either? I am sure I am missing something simple, because I would think you could/should be able to trigger a function from a viewmodel externally somehow?

EDITJust for reference, the models DO NOT overlap..

Thanks in advance, David.

You can merge view models in an object like this:

var mainVModel = {
    vModel1: { // has save method},
    vModel2: { // has save method},
    saveAll : function(){;;

ko.applyBindings(new mainVModel());
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » Knockoutjs saves multiple view models from a single function?

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录