Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone Custom Events

Let's say I have two views (paginationview and postsview) and a collection (postscollection). Whenever the .next button is clicked in the paginationview I am calling the next function in the postscollection and the post collection is fetching the new page's posts from the server (code is simplified). Now in my post view, I only want to display the posts that are in the last page. I don't want to bind my view to the 'add' event in the collection because there are more cases that something is 'added' to the collection. I want my 'renderlist' function in my postsview only to be called when the 'nextPage' function is called in my postscollection. How do I connect these to functions together?

// paginationview

var PaginationView = Backbone.View.extend({
    events:{
        'click a.next' : 'next',
    },

    next: function() {
        this.collection.nextPage();
        return false;
    }
});

// collection

var PostsCollection = Backbone.Collection.extend({
    model: model,

    initialize: function() {
        _.bindAll(this, 'parse', 'url', 'pageInfo', 'nextPage', 'previousPage');
        this.page = 1;
        this.fetch();
    },

    parse: function(response) {
        console.log(response);
        this.page = response.page;
        this.perPage = response.perPage;
        this.total = response.total;
        this.noofpages =response.noofpages;
        return response.posts;
    },

    url: function() {
        return '/tweet/' + '?' + $.param({page: this.page});
    },

    nextPage: function() {
        console.log("next page is called");
        this.page = this.page + 1;
        this.fetch();
    },

// postsview

var PostsView = Backbone.View.extend({
    events:{
        'click #testbutton' : 'test',
        'click #allbutton' : 'render',
    },

    initialize: function() {
        this.listenTo(this.collection, 'add', this.addOne);
    },

    render: function() {
        $(".maincontainer").html("");
        this.collection.forEach(this.addOne, this);
        return this;
    },

    renderlist: function(){
        $(".maincontainer").html("");
        this.collection.forEach(this.addOne, this);
    },

    addOne: function(post) {
        var post = new postView({model : post});
        post.render();
        this.$el.prepend(post.el);
    },
});
like image 891
s_curry_s Avatar asked Jul 10 '13 19:07

s_curry_s


People also ask

Is Backbone JS still used?

Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.

Is backbone a MVC?

BackboneJS is a lightweight JavaScript library that allows to develop and structure the client side applications that run in a web browser. It offers MVC framework which abstracts data into models, DOM into views and bind these two using events.


1 Answers

You can use your own event for this purpose.

In Collection.nextPage you fire the event:

this.trigger('nextpage');

and in view in initiazlie method you bind your function to this event:

this.listenTo(this.collection, 'nextpage', this.renderlist);

And also don't forget to bind context of renderlist to this (again in initialize method of view):

_.bindAll(this, 'render', 'rederlist');
like image 166
Artem Volkhin Avatar answered Oct 27 '22 23:10

Artem Volkhin