Note that there are some explanatory texts on larger screens.

plurals
  1. POAm I overusing the Knockout mapping plugin by always using it to do my viewmodel?
    primarykey
    data
    text
    <p>I'm still learning the proper usage of Knockout and I've found myself quickly getting away from ever typing <code>ko.observable</code> when setting up my viewmodel and instead just defining an object literal and passing it through the mapping plugin with something like </p> <pre><code>var viewModel = ko.mapping.fromJS(data); </code></pre> <p>or at the very least, something along the lines of stuffing all of my data into an attribute on the viewModel like so</p> <pre><code>var viewModel = { ... events etc ... , "data": ko.mapping.fromJS(data) } </code></pre> <p>To be honest, the main reason I've been doing this is to get around having to type <code>ko.observable</code> and <code>ko.observableArray</code> repetitively. I'm just trying to figure out if this is a good approach and if there are any downsides to dropping the specific <code>var x = ko.observable()</code> declaration all together. Also, I'm doing this all on load, not in response to any ajax call etc, which from what I can tell, is what the mapping plugin was designed for.</p> <p>In your work with knockout, do you still declare the observables manually, one by one, or have you gone with the mapping.fromJS method that I use? Are there any specific downsides to using the mapping plugin so frequently like this? </p> <p>Edit:</p> <h2>Specific Example</h2> <p><a href="http://blog.stevensanderson.com/2010/07/12/editing-a-variable-length-list-knockout-style/">In this article</a>, Steve sets up his viewModel by doing</p> <pre><code>var initialData = [ { ... } , { ... } ]; // json from the serializer var viewModel = { gifts : ko.observableArray(initialData) }; </code></pre> <p>Normally, I'd just use <code>ko.mapping.fromJS</code> for this situation as well, specifically to make sure the objects within the array are turned into observables as well. Looking at what he did, my approach seems like its overkill and adds a bit of unnecessary overhead.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload