Hello-World
Hello-World

Reputation: 9555

every time the button is pressed clear the view and start again - backbone.js

Every time I press the button it adds my data and reloads the page and the list disappears. The preventdefault worked but then every time I click the button it keeps adding to the list.

How do I make it clear the list and start again every the button is clicked?

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="Scripts/jquery-1.7.2.js"></script>
    <script type="text/javascript" src="Scripts/underscore.js"></script>
    <script type="text/javascript" src="Scripts/backbone.js"></script>

</head>
<body>

    <div>

        <div id="container">
            <button>Load</button>
            <ul id="list"></ul>
        </div>

        <div id="list-template">
            <li><a href=""></a></li>
        </div>


        <script type="text/javascript" src="JavaScript.js"></script>


    </div>

</body>
</html>

JavaScript

model = new Backbone.Model({

    data: [

        { text: "site 1", href: "link1.htm" },
        { text: "site 2", href: "link2.htm" },
        { text: "site 3", href: "link3.htm" },
        { text: "site 4", href: "link4.htm" },
        { text: "site 4", href: "link4.htm" },
        { text: "site 4", href: "link4.htm" },
        { text: "site 4", href: "link4.htm" },

    ]
});

var View = Backbone.View.extend({

    initialize: function () {
        //console.log('initializing ' + this.options.blankOption);
        this.template = $('#list-template').children();
    },
    el: '#container',
    events: {
        'click button' : 'render'
    },
    //model:model,
    render: function(event){
        //event.preventDefault();
        var data = this.model.get('data');
        for (var i = 0; i < data.length; i++) {
            var li = this.template.clone().find('a').attr('href', data[i].href).text(data[i].text).end();
            this.$el.find('ul').append(li);
        }



    }
});

Upvotes: 1

Views: 402

Answers (2)

guzmonne
guzmonne

Reputation: 2540

Another thing you should try is instead of creating a model with a data attribute full of objects you could create a Collection called something like "Links" and a model called "Link". To show the models you can pass the collection to the view and on the render call:

this.collection.each(function(model){$el.find('ul').append('<li><a href="' + model.get("href") + '">' + model.get('text') + '</a></li>')})

You can improve this by creating another view just to display the model with it's "tagName" attribute set to "li".

For example, here i'll create a model and a collection to hold the models. Then I'll create 2 views one to hold the collection and the other to display the model.

var Link = Backbone.Model.extend({})
var Links = Backbone.Collection.extend({
  model: Link
  url: '/links'

});

var ls = new Links()

When you call ls.fetch() a call will be made to your server that should answer with a JSON object with all your links. Backbone will take care to instantiate the Link models within itself. You can check if it worked by doing ls.length which should return a value bigger than zero.

Then you could create a View like this:

var MainView = Backbone.View.extend({
  template: JST['teams/index']

  render: function(){
      $(this.el).html(this.template())
      this.collection.each(this.appendLink())
      this},
  appendTeam: function(model){
    view = new LinkView({model: model})
    $('#links').append(view.render().el)
    this}})

The template function should have a list whith id = links where we can append our models. All we have to do now is create a the LinkView

var LinkView = Backbone.View.extend({
  template: JST['teams/team'],
  tagName: 'li',

  render: function(){
    $(this.el).html(this.template({model: this.model}));
    this}})

Hope this helps!

Upvotes: 1

yuwang
yuwang

Reputation: 876

The reason is each time you clicked the button, the whole view is rendered again and appended to the existing content. this would fix your problem:

// empty the content inside the $el and re-rendering.
this.$el.empty();
this.$el.find('ul').append(li);

Upvotes: 1

Related Questions