Issue with computed item in knockout

65 views
Skip to first unread message

Bharat Gillala

unread,
Mar 12, 2015, 4:08:18 PM3/12/15
to knock...@googlegroups.com

I am performing paging using knockout.js .The problem is

     JobActivity = function ()
    {
    var self = this;

    self.arraytoadd = ko.observableArray(); 
     /This is the array i am putting the data in  from ajax call 
    self.currentPage = ko.observable(1);
    self.perPage = 5;  
    self.pagedItems = ko.computed(function () {
        self.dummy();
        var pg = this.currentPage(),
        start = this.perPage * (pg - 1),
        end = start + this.perPage;

        return self.arraytoadd().slice(start, end);
    }, this);

    self.nextPage = function () {
        if (self.nextPageEnabled())
            this.currentPage(this.currentPage() + 1);
    };
    self.nextPageEnabled = ko.computed(function () {
        return this.arraytoadd().length > this.perPage * this.currentPage();
    }, this);

    self.previousPage = function () {
        if (this.previousPageEnabled())
            this.currentPage(this.currentPage() - 1);
    };

    self.previousPageEnabled = ko.computed(function () {
        return this.currentPage() > 1;
    }, this);
} //END OF DECLARING JOBACTIVITY 

-------------------------jquery dialog------------

1)click a link ,jquery dialog opens and assigns the data to arraytoadd.

This paging is in a juery dialog.Every time i navigate to next page and cancel the dialog and reopen the dialo it is not showing page1 but instead page2. ISSUE I AM HAVING IS PAGEDITEMS above is not refreshing after i cancel the dialog.Can anyone help me how do i enforce pageditems to start from page1 always when it opens.

Hasen el Judy

unread,
Mar 12, 2015, 10:24:27 PM3/12/15
to knock...@googlegroups.com
I don't know what's happening with the dialog but it sounds like you're taking values from it prematurely. If you have an option to cancel the dialog, then don't take the "output" of the dialog until the user hits ok.

Noirabys

unread,
Mar 16, 2015, 12:15:29 PM3/16/15
to knock...@googlegroups.com
please post the dialog code .. perhaps jquery dialog creates new dom elements ?

Ian Yates

unread,
Mar 17, 2015, 7:57:41 PM3/17/15
to knock...@googlegroups.com
It also sounds like your view model is hanging around.  Are you reusing the same instance rather than a whole new one?  Could you post the code that creates your dialog?

I suspect the dialog itself is a red-herring and that if you just bound the output of ko.toJSON on your viewmodel to a <div> on your page you'd see it also displays the wrong data.
Reply all
Reply to author
Forward
0 new messages