I am using the backbone for the application that I am creating. In this application, I have a main view that displays a template with two other views inside. One heading and another with some content. The header view is simply used to interact with the content view and has certain functions.
In the header template and content template, I have the same piece of code, a hidden DIV with the image of the bootloader that is displayed when making an ajax call. The problem is that when I download the application for the first time (or when I update the content view), the content view loads some data from the ajax request, but the loader appears both in the header and in the template content (for example, if ajaxStart () is a global event that is not tied to a view.
Here is the content setting:
App.View.Content = Backbone.View.extend({ type:'test', template: twig({ href: '/js/app/Template/Content.html.twig', async: false }), block:{ test:twig({ href: '/js/app/Template/block/test.html.twig', async: false }) }, list:[], showLoader: function(el){ console.log('loader: ', $('#ajax_loader', el)); $('#ajax_loader', el).show(); console.log('Ajax request started...'); }, hideLoader: function(el){ $('#ajax_loader', el).hide(); console.log('Ajax request ended...'); }, initialize: function(params) { this.el = params.el; this.type = params.type || this.type; var self = this; this.el .ajaxStart(function(){self.showLoader(self.el);}) .ajaxStop(function(){self.hideLoader(self.el);}); this.render(function(){ self.list = new App.Collection.ListCollection(); self.refresh(1, 10); }); }, refresh:function(page, limit) { var self = this; console.log('Refreshing...'); $('#id-list-content').fadeOut('fast', function(){ $(this).html(''); }); this.list.type = this.type; this.list.page = page || 1; this.list.limit = limit || 10; this.list.fetch({ success: function(data){
As you can see, I use the ugly part of the code to attach the ajaxStart / ajaxStop event:
this.el .ajaxStart(function(){self.showLoader(self.el);}) .ajaxStop(function(){self.hideLoader(self.el);});
I use it like this:
this.el .ajaxStart(self.showLoader()) .ajaxStop(self.hideLoader());
But for some reason, which is still undefined at my end, this.el not defined in showLoader() and hideLoader() .
I thought that ajaxStart() and ajaxStop() were attached to the this.el DOM and that only this view would be able to listen to it. But my View header, which has exactly the same setting (except for the loaded branch template), apparently receives the event and shows the loader.
To make sure of this, I commented on showLoader() in the content view and the loader is still showing in the header view.
I don't know what I'm doing wrong :(
EDIT (after responding with "mu too short"):
my content view now looks like this:
showLoader: function(){ //this.$('#ajax_loader').show(); console.log('Ajax request started...'); }, hideLoader: function(){ this.$('#ajax_loader').hide(); console.log('Ajax request ended...'); }, initialize: function(params) { var self = this; console.log(this.el); _.bindAll(this, 'showLoader', 'hideLoader'); this.$el .ajaxStart(this.showLoader) .ajaxStop(this.hideLoader); this.render(function(){ self.list = new App.Collection.List(); self.refresh(1, 10); }); }, ... render: function(callback) { console.log('Rendering post by page...'); this.$el.html(this.template.render({ })); if (undefined != callback) { callback(); } }
and my title:
... showLoader: function(){ this.$('#ajax_loader').show(); //console.log('Ajax request started...'); }, hideLoader: function(el){ this.$('#ajax_loader').hide(); console.log('Ajax request ended...'); }, initialize: function(params) { var self = this; _.bindAll(this, 'showLoader', 'hideLoader'); this.$el .ajaxStart(this.showLoader) .ajaxStop(this.hideLoader); this.models.Link = new App.Model.Link(); this.render(); }, render: function(callback) { this.$el.html(this.template.render({ data: [] })); if (undefined != callback) { callback(); } } ...
But the loader is still showing in the header view template
PS: this.showLoader() not a typo, since I wanted to call a function in the current trunk mode.