0

I'm trying to achieve something like this:

for (var i = 0; i < types.length; i++) {
    var type = types[i];
    myObj()['text' + type.name] = ko.computed(function () {
        return myFuction(myObj()[type.name + 'MyText']());
    });
}

If that's not clear, I have a certain object type, for which I am trying to achieve maximum flexibility. Namely, I want to be able to add a new attribute to my object in the DB, without having to change the client side code.

This works fine except when it comes to computed variables. In the above example, the function seems to always be called on the last value of type, ie the last value of the array.

Anyone has a solution to this issue?

Example:

types = [{name: 'red'}, {name: 'green'}, {name: 'blue'}];

In the end, myFunction and thus the computed variable always gets called on {name: blue} Hope this is kinda clear...

Nicolas
  • 356
  • 1
  • 5
  • 17
  • A sample fiddle will go a looooonnggg way! – Sujesh Arukil May 15 '13 at 13:50
  • Making a fiddle for this is not that easy... – Nicolas May 15 '13 at 13:58
  • 1
    Imagine how hard it is to answer your question then when you can't give us a simple example of your model =P – Matthew Cox May 15 '13 at 14:24
  • Well well well, this is the closest I could come up with, and for some reason, it works... http://jsfiddle.net/hksbR/ Could not figure out the difference though, but interesting to see this work. – Nicolas May 15 '13 at 14:51
  • Are you sure that your computeds are not using deferEvaluation? – nemesv May 15 '13 at 15:01
  • Yes I am sure, I added deferEvaluation to false to confirm but that didn't change the outcome – Nicolas May 15 '13 at 15:14
  • If you don't want to share your actual code nor you cannot repro this issue in jsfiddle it will be very hard to help... by the way if you use deferEvaluation: true in your jsfiddle. It will print out 3 blues... so I your actual code you have a similar problem so the computeds are not executed right away... – nemesv May 15 '13 at 15:18
  • The actual code really is very close to this. But I suspect it might have to do with Durandal, and the way bindings get applied by that library. The code above is defined in an activate function, and seems to break only after everything is loaded. – Nicolas May 15 '13 at 15:30
  • I can't help but think an observable array with objects that are dynamically created would be a better fit than having a single object `myObj` that accepts a parameter (color) and simply returns that color. Could you not have an object with two observables; one for name, one a computed based on its display value - then populate this with your data from the DB as your application is initialized? – Patrick D May 15 '13 at 16:36
  • 1
    @Nicolas If you haven't resolved this issue yet then try to wrapping the whole body of your `for` loop into an immediately executed function like in this sample: http://jsfiddle.net/R8k2c/ – nemesv May 24 '13 at 15:36
  • In the end I went the jQuery way to achieve this. But thanks everyone for helping me on this. – Nicolas Jun 25 '13 at 10:10

0 Answers0