Note that there are some explanatory texts on larger screens.

plurals
  1. PORails + Backbone.js: Having trouble accessing objects in view
    primarykey
    data
    text
    <p>got a problem for a couple of days with backbone and can't figure out a way to solve it or find a solution on stackoverflow.</p> <p>I use Rails 3.1 + latest backbone (+ coffeescript):</p> <p>Here is one of my views as an example (same problem in all of them):</p> <pre><code>Blog.Views.Payment ||= {} class Blog.Views.Payment.PaymentView extends Backbone.View className: 'payment_method' tagName: 'td' events: 'click #renderPayment': 'renderPayment' initialize: -&gt; # CAN'T access @options.payment_methods here @options.payment_methods.bind(###stuff###) render: -&gt; # CAN'T access @options.payment_methods here either... $(@el).html ("### something with #{@options.payment_methods or @options.payment_methods.model}") return @ updateView: ()-&gt; # updating view stuff... renderPayment: -&gt; # ACCESSING @options.payment_methods fine here!!! if ($("#payment_details").length == 0) $(@el).append("&lt;ul id='payment_details'&gt; &lt;li id='payment_type'&gt;#{@options.payment_methods.get(1).get('payment_type')}&lt;/li&gt; &lt;/ul&gt; ").effect("highlight", 700) </code></pre> <p>In the first two cases when I run the example the browser tells me that @options.payment_methods is not defined, third case working fine.</p> <p>Second thing is that I can't access any DOM elements that are already "hardcoded" into the page and are not created by the Javascript. I know the reason for that and have been reading a loooot of posts here on Stackoverflow about it but I couldn't get any solution to work. Any hints greatly appreciated.</p> <p>Best regards, Phil</p> <p>EDIT: It seems that it has something to do with the timing of access of the objects on the page, similiar to the access of the hardcoded DOM elements of the page. If I edit my view as follows I can't access @options.payment_methods anymore even if I was able to before at that point in the code.</p> <pre><code># changed "renderPayment" to "$(document).ready" or just "$" in coffeescript $ -&gt; # ACCESS NOT possible anymore if ($("#payment_details").length == 0) $(@el).append("&lt;ul id='payment_details'&gt; &lt;li id='payment_type'&gt;#{@options.payment_methods.get(1).get('payment_type')}&lt;/li&gt; &lt;/ul&gt; ").effect("highlight", 700) </code></pre> <p>EDIT2: added my corresponding router file: (this is simply a modified version of the backbone-rails gem example "Blog")</p> <pre><code>class Blog.Routers.PostsRouter extends Backbone.Router initialize: (options) -&gt; @posts = new Blog.Collections.PostsCollection() @posts.reset options.posts # ... other collections # fetch payment_methods collection @payment_methods = new Blog.Collections.PaymentMethodsCollection() @payment_methods.reset options.payment_methods @payment_methods.fetch() @model = ({posts: @posts, mails: @mails, addresses: @addresses, purchases: @purchases, payment_methods: @payment_methods}) routes: "/new" : "newPost" "/index" : "index" "/:id/edit" : "edit" "/:id" : "show" ".*" : "index" # main view index: -&gt; # render Product Info View @view = new Blog.Views.Product.ProductView(purchases: @purchases) $("#product_1").html(@view.render().el) ##### view etc. # render Payment View @view5 = new Blog.Views.Payment.PaymentView(payment_methods: @payment_methods) $("#customer_1").append(@view5.render().el) ### other views... </code></pre> <p>And my Payment model:</p> <pre><code>class Blog.Models.PaymentMethod extends Backbone.Model paramRoot: 'payment_method' defaults: payment_type: null # ... class Blog.Collections.PaymentMethodsCollection extends Backbone.Collection model: Blog.Models.PaymentMethod url: '/payment_methods' </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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