Weird view attribute result in backbone

I have a view in backbone and I would like to create some attributes.

I did that:

  • How to convert 1 to true or 0 to false upon model fetch
  • How can I hover on a rectangle in javascript?
  • Sorting collections using Backbone.js
  • Wierd text: “#_=_” showing on the URL after successful Login to Facebook with spring social
  • Backbone.js tutorials and learning resources
  • External templates and “el” with Knockout and RequireJS?
  • var ProgrammeDetailsView = Backbone.View.extend({
    
        $infoResult: $('#info-result'),
        $castingResult: $('#casting-result'),
        $broadcastResult: $('#broadcast-result'),
        $testResult: $('#test'),
    
    [...]
    

    And I surprisely got this result:

     console.log(this.$infoResult); -> Array { selector="#test", forEach=forEach(), reduce=reduce(), more...}
    

    As you can see, I’ve used “this.$infoResult” which should return an object with id “#info-result” but it’s in fact the latest variable which is returned.

    If I do that:

    var ProgrammeDetailsView = Backbone.View.extend({
    
        $infoResult: '#info-result',
        $castingResult: '#casting-result',
        $broadcastResult: '#broadcast-result',
        $testResult: '#test',
    

    I’ve got no problem, cf:

    console.log(this.$infoResult); -> "#info-result"
    

    Do you have any idea why?

  • Edge Browser: document.execCommand(“copy”) command is not working in AJAX success callback
  • Collection add event listener in Backbone
  • How to structure a Node, Express, Connect-Auth and Backbone application on the server-side?
  • Why instanceof keyword does not work with Backbone js models?
  • Frontend javascript frameworks with node.js
  • Nested Models in Backbone.js, how to approach
  • One Solution collect form web for “Weird view attribute result in backbone”

    Are you sure the elements that you are referring to are available when the object is being intialized? A lot of the time, the view that may be creating this object, or the HTML that contains it may not have already modfied the DOM, so you won’t get a reference to it.