Sunday, 15 January 2012

knockout.js - Knockout computed observable in hottowel SPA default viewmodel -


I'm trying to calculate a simple knockout after following the view-model pattern in the Hottowel SPA template. What is the best way to do this?

I was basically something like this:

  define (['services / logger'], function (logger) {var vm = {active: active, title} : 'Home view', testfield: co.sevable ("this is an exam"), test computed field: getcomputed (this)}; return vm; / / # field internal methods function getcomputed () {return "calculation : "+ This.testField ();} activate the function () {logger.log ('see home active', empty, 'home', true); true back;} // # endregion});   

But this is the result of an error, although I'm not 100% sure why

  TypeError: this.testField function is not  < / Pre> 

Then with some testing and error I found this:

  define (['service / logger'], function (logger) {var vm = {activate} : Activate, title: 'home view', testField: ko.observable ("this is a test"); vm.testComputedField = ko.computed (getComputed, VM); return VM; // # field internally Work getComputed () {Return "computed:" + this.testField ();} Nkshn activate () {logger.log ( 'Home View active "empty" house ", true);) integrity back;} # # endregion});   

But I'm not sure this is a very beautiful way of doing this; I'm not clearly talking about the module patterns used to look at the hot-towel models. So my questions are:

Why does not my original method work? Are there any better or alternative ways to define visualmodel than my second method?

Mutes, this is the normal pattern I'm using, it's working well for me

I use focus expex and local scocked variable for KO binding, and job announcements for Durandal specific methods (Active, View Attach, etc.). VM object function must be after expression.

  define (['service / logger'], function (logger) {var testField = ko.observable ("this is an exam"); ​​var getcomputed = ko.computed (function ( ) {Return "computed:" + activate function testfield ();) {logger.log ('home view active', blank, 'home', true);;} var vm = {active: active , Title: 'home view', testfield: testfield, test computed field: getcomputed}; return vm;});   

Note: This hottowel is for SPA template

No comments:

Post a Comment