1

I was trying the solutions that suggest in this questions but it don't work

How to pass parameters to a view

How can I pass a parameter into a Backbone.js view?

Backbone.js - passing arguments through constructors

I am trying to pass extra parameter to use in my template, I read that all extra params that I pass I can have access to them in this.options.varName but it dont work for me.

heres the controller code:

servicios : function(){
    //Crea la colecion de tipos de servicios municipales

    //Crea el modelo para dar de alta un nuevo reporte de servicio municipal
    var reporteServicioSingle = new ReporteServicioSingle();

    //Crea la vista para la pantalla de servicios municipales
    var servicios_view = new Servicios({
        model      : reporteServicioSingle,
        collection : this.reportes,
        tipos      : this.tipos
    });

    // Carga la vista renderisada y inicializa foundation
    $("#app-content").html(servicios_view.render().el);
    $(document).foundation();
},

And this is my view code:

render: function(){
    console.log(this.options.tipos);
    this.$el.html(Handlebars.templates.servicios(this.collection));
    $(document).foundation();
    return this;
},

but the chrome developers console, send me an error that options is not definided.

Uncaught TypeError: Cannot read property 'tipos' of undefined 

Thank you and sorry for my bad english.

Community
  • 1
  • 1
kentverger
  • 475
  • 1
  • 5
  • 19
  • possible duplicate: [Backbone 1.1.0 Views - Passing Options](http://stackoverflow.com/q/19325323/722238). – fbynite Dec 23 '13 at 17:36

1 Answers1

1

Assuming you're using Backbone 1.1, it no longer attaches the options argument to this.options automatically anymore, a shitty change (imho).

You'll need to extend the options parameter yourself in your initialization method:

initialize: function(options) {
    this.options = _.omit(options, ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName', 'events']);
}
James Lai
  • 2,051
  • 17
  • 14
  • Why not pull out precisely the options your view needs so that your view's interface is at least specified in one place? – mu is too short Dec 23 '13 at 18:09
  • ...because that would be terrible for maintainability? – James Lai Dec 23 '13 at 18:34
  • A well specified interface is terrible for maintainability? What? Not knowing an object's interface is terrible for maintainability. – mu is too short Dec 23 '13 at 18:37
  • @muistooshort ¿What that you mean with pull out the options? – kentverger Dec 23 '13 at 18:49
  • @kentverger He means rather than extending everything from the `options` parameter, to only pick out what you intend to use. My guess is he a programmer used to a strongly typed language. However, convention with Javascript is to not create rigid interfaces (hence the complete lack of interfaces). – James Lai Dec 23 '13 at 19:00