Running Backbone view initialize function from another view

41 views
Skip to first unread message

Kris D

unread,
Jul 17, 2018, 7:43:23 AM7/17/18
to backbonejs

I need to execute this Backbone view initialize function in another view, to fetch the collection and render it again, but I'm not quite sure how to do this?


Here's the view with initialize function that I need to run in another Backbone view:


Checkout.Views.ShippingTypes = Backbone.View.extend({
   el: null,

   initialize: function(options) {
     // Get the seller_id
     this.seller_id = options.seller_id;
     this.company = options.company;
     this.premium = options.premium;
     this.address = options.address;
     this.postcode = options.postcode;
     this.payment_method = $('#payment-methods').find('li.selected-method').find('input.payment-name').val();

     // When fetching is done call the addAll function to start the rendering process
    this.listenTo(this.collection, 'reset', this.addAll);

    // Fetch the data for shipping types
    if (!App.isObject(Checkout.shippingMethods)) {
       Checkout.shippingMethods = {};
    }

    if (Checkout.shippingMethods[this.seller_id] && Checkout.shippingMethods[this.seller_id].readyState < 4) {
        Checkout.shippingMethods[this.seller_id].abort();
    }

    Checkout.shippingMethods[this.seller_id] = this.collection.fetch({
        reset: true,
        data: {
            company: this.company,
            premium: this.premium,
            shipping: Checkout.selectedShippingMethod,
            address: this.address,
            postcode: this.postcode,
            is_shipping_selected: Checkout.isShippingSelected || false,
            payment_method: this.payment_method
        }
    });
  }
});



John Stile

unread,
Jul 17, 2018, 2:06:42 PM7/17/18
to backb...@googlegroups.com
My understanding is that you should tare down all backbone objects when
switch views, to avoid memory issues.

If you need a collection in multiple views, in each view's initialize
will instantiate a new collection instance.

On 07/17/2018 04:43 AM, Kris D wrote:
>
> I need to execute this Backbone view initialize function in another
> view, to fetch the collection and render it again, but I'm not quite
> sure how to do this?
>
>
> Here's the view with initialize function that I need to run in another
> Backbone view:
>
>
> |Checkout.Views.ShippingTypes=Backbone.View.extend({el:null,initialize:function(options){//
> Get the seller_idthis.seller_id =options.seller_id;this.company
> =options.company;this.premium =options.premium;this.address
> =options.address;this.postcode =options.postcode;this.payment_method
> =$('#payment-methods').find('li.selected-method').find('input.payment-name').val();//
> When fetching is done call the addAll function to start the rendering
> processthis.listenTo(this.collection,'reset',this.addAll);// Fetch the
> data for shipping
> typesif(!App.isObject(Checkout.shippingMethods)){Checkout.shippingMethods
> ={};}if(Checkout.shippingMethods[this.seller_id]&&Checkout.shippingMethods[this.seller_id].readyState
> <4){Checkout.shippingMethods[this.seller_id].abort();}Checkout.shippingMethods[this.seller_id]=this.collection.fetch({reset:true,data:{company:this.company,premium:this.premium,shipping:Checkout.selectedShippingMethod,address:this.address,postcode:this.postcode,is_shipping_selected:Checkout.isShippingSelected
> ||false,payment_method:this.payment_method }});}});|
> ||
>
>
>
> --
> The Unofficial Backbone.js Group
>  
> Job Board:
> https://groups.google.com/forum/#!topic/backbonejs/wHRdZczyOEc
> <https://groups.google.com/forum/#%21topic/backbonejs/wHRdZczyOEc>
> ---
> You received this message because you are subscribed to the Google
> Groups "backbonejs" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to backbonejs+...@googlegroups.com
> <mailto:backbonejs+...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages