Thursday, 15 March 2012

Using multiple viewModels within knockout.js -



Using multiple viewModels within knockout.js -

i'd maintain knockout script more organized, moreover, i'd avoid naming 2 functions same thing accident. wondering if nest viewmodels in same function (i kept real simple): fiddle

here's html

<p>first name: <strong data-bind="text: other.firstname">todo</strong></p> <p>last name: <strong data-bind="text: other.lastname">todo</strong></p> <p>full name: <strong data-bind="text: other.fullname">todo</strong></p>

and js:

function appviewmodel() { var self = this; self.other = { firstname: ko.observable("bert"), lastname: ko.observable("bertington"), /*fullname: ko.computed(function(){ homecoming this.firstname + " " + this.lastname; }, this)*/ } }

this works fine, if uncomment ko.computed it'll crash. there way organize knockout in way, why computed crashing, there way write ko.computed function work?

edit: problem #2

if have form form this:

<form data-bind="submit: other.othersubmit" data-ajax="false"> <button type="submit">submit</button> </form>

and add together handler submit so:

// simple *viewmodel* - javascript defines info , behavior of ui function appviewmodel() { var self = this; self.other = new function(){ var self = this; self.firstname = ko.observable("bert"); self.lastname = ko.observable("bertington"); self.fullname = ko.computed(function(){ homecoming self.firstname() + " " + self.lastname(); }); self.othersumbit = function(){} } } // activates knockout.js ko.applybindings(new appviewmodel());

why error console homecoming this:

the value submit binding must function

can seek this:

// simple *viewmodel* - javascript defines info , behavior of ui function appviewmodel() { var self = this; self.other = new function(){ var self = this; self.firstname = ko.observable("bert"); self.lastname = ko.observable("bertington"); self.fullname = ko.computed(function(){ homecoming self.firstname() + " " + self.lastname(); }); } } // activates knockout.js ko.applybindings(new appviewmodel());

knockout.js

No comments:

Post a Comment