Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, but you'd have to output the variable with erb e.g.</p> <pre><code>ko.observable("&lt;%= @person.name %&gt;"); </code></pre> <p>Generally however, you don't want to pass specific variables to specific observables, you want to pass the resource itself to a new I stance of the view model, and then set the view model variables from that, I will update with a better example when I get to a computer, but basically:</p> <pre><code>ko.applyBindings(new viewModel("&lt;%= resource.to_json.html_safe %&gt;")); </code></pre> <p>Update with better example</p> <p>A person view model I would have structured roughly like this, note the parenthesis, Im not sure exactly why they are needed but if I remember correctly it doesen't work without them.</p> <pre><code>var personViewModel = function(person){ var self = this; self.first_name = ko.observable(person.first_name); self.last_name = ko.observable(person.last_name); self.friends = ko.observableArray(person.friends); } person_json = &lt;%= (@person.to_json.html_safe) %&gt;; ko.applyBindings(new personViewModel(person_json)); </code></pre> <p>However, I like to use coffeescript for my view models these days, as a class for each view model, here is the same-ish code (working, rough equivalent, using class instead of funciton) in coffeescript.</p> <p>view_models/personviewmodel.js.coffee</p> <pre><code>class PersonViewModel constructor: (person) -&gt; @first_name = ko.observable(person.first_name) @last_name = ko.observable(person.last_name) @friends = ko.observableArray(person.friends) window.PersonViewModel = PersonViewModel </code></pre> <p>And then in your actual view below the form its being attached to.</p> <pre><code>person_json = &lt;%= (@person.to_json.html_safe) %&gt;; ko.applyBindings(new PersonViewModel(person_json)); </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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