Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sending wildcards to the viewmodel with requireVM #100

Closed
maxfridbe opened this issue Mar 8, 2013 · 1 comment
Closed

sending wildcards to the viewmodel with requireVM #100

maxfridbe opened this issue Mar 8, 2013 · 1 comment
Labels

Comments

@maxfridbe
Copy link

I am following the common requieVM pattern and am having an issue accessing the property set.

<div class="viewServer" data-bind="page:{id:'viewServer'}" >
                <div data-bind="page:{id:'?', nameParam:'Address', sourceOnShow:'/views/servers/viewServer.html', withOnShow:requireVM('/viewmodels/servers/viewServerViewModel.js')}">

                </div>
            </div><!--END PAGE-->

This hit's twice in pagerjs:

 if (this.ctx[nameParam]) { // set observable ...
                            this.ctx[nameParam](this.currentId);
                        } else { // ... or create observable
                            this.ctx[nameParam] = ko.observable(this.currentId);
                        }

First it creates a new observable because context is not set.
Then it sets the observable.

In the VM doing:

 self.Address.subscribe(function(newval){
            alert(self.Address());
        });

Does nothing, as the self.Address gets overwritten.

When trying to get to #/viewserver/ipaddress

@finnsson
Copy link
Owner

finnsson commented Mar 8, 2013

Thanks for the bug report.

I have limited time to check for the solution at the moment. I guess pagerjs need to defer the creation/binding of currentId until the view model is loaded or maybe merge the created observables with the lazy-loaded view-model somehow.

A workaround until I've fixed this might be to use bind (undocumented feature at the moment) that can be used like

var viewModel = {
  myPage: ko.observable()
};

<div data-bind="page: {id: 'foo', bind: myPage}"></div>

where the pager.Page-object will be bound to the observable myPage. From there you should be able to fetch the currentId (you can use getCurrentId which is an observable to the current ID).

Another tip is to use navigationFailed (http://pagerjs.com/demo/#!/navigation/failed_navigation) to create child-pages on-demand instead of using id: '?'. There are some trade-offs between the different solutions (using navigationFailed makes it possible to animate between page transitions - on the downside new dom-elements are created and the code is at the moment a bit more verbose).

Hope this helps.

finnsson added a commit that referenced this issue Oct 19, 2013
Do not overwrite nameParam on view model if it is already defined (fixes #100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants