简体   繁体   中英

Best way to call view from another view and pass it a model in Backbone

how can I call a view from another view and pass it a model?Below in my code I would call another view at #button_cont click as showed in events.Can I simply call the new view and pass it model or exists a better way?

Below a caller view:

define(["jquery", "underscore","Handlebars","models/attore",
"text!templates/backend0.html"],
function ($, _, Handlebars,Attore,template) {

var BackendView0 = Backbone.View.extend({



    template: Handlebars.compile(template),


    events: {

        'click #button_cont': 'twitter_list',

    },



    initialize: function () {


    },

    render: function (eventName) {


       $(this.el).html(this.template());

      return this;


    },



     twitter_list: function () {
        var nome=$('#nome').val();
        var cognome=$('#cognome').val();

        var attore= new Attore({nome:nome,cognome:cognome});

   ---  Here I want call another view and pass it model attore---------
    },


  });

return BackendView0;

If it will always be only the 2 views you could tell them about each other by storing a reference to another view. Then the method to pass a model could do something like this:

this.otherView.model = this.model;

This reference can be stored on the view initialization or through a setter method:

setOtherView: function(view){
    this.otherView = view;
}

You don't say whether your two views are separate from each other or part of a hierarchy. If the views are truly separate, then they shouldn't be "calling" each other at all. Each view should manage its own part of the page independent of any parts of the page.

If the views are part of a hierarchy (eg a collection view and a model view), then the parent view should be responsible for creating the child view, in which case it can pass the model as input to the constructor just like any other view.

我会这样做:

var myNewView = new AnotherView({ model: attore });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM