Jonas Geiregat
Jonas Geiregat

Reputation: 5432

backbone js , binding model to view

I'm trying out backbonejs but got stuck on how to bind the model to the view.

yepnope({
    load : ["/static/js/lib/jquery-1.6.2.min.js", "/static/js/lib/underscore-min.js", "/static/js/lib/backbone-min.js"],
    complete: nameList
});

function nameList() { 
    var PageItem = Backbone.Model.extend({
                                    defaults: {name: "default name" }
    }); 
    var Page = Backbone.Collection.extend({
        model: PageItem 
    });     
    var page = new Page;

    var AppView = Backbone.View.extend({
        el: $("#names"),
        $artistList: $('#names_list'),
        $inputField: $('input#new_name'),
        events: { 
            "keypress input": "processKeyPress" 
        },
        processKeyPress: function(event){
            if(event.charCode == 13) {
                event.preventDefault();
                this.addName();   
             }   
        }, 
        addName: function(event) {
                   var newName = this.$inputField.val(); 
                   this.$artistList.prepend('<li>' + newName + '</li>');
                   page.push(new  PageItem({name: newName})); 
                   // I've also tried page.push({name: newName});                                                                                                                                                  
    }       }); 
    var app = new AppView;}

When I press enter on the input field, it runs processKeyPress which calls addName, the new name is added the the html list but not pushed onto the model. I keep getting:

Uncaught TypeError: Object function (a){return new l(a)} has no method 'isObject' 

Upvotes: 2

Views: 1858

Answers (1)

lecstor
lecstor

Reputation: 5707

ok, please test this out for yourself, but it appears to work with the Github version of underscore so maybe there was a bug which has been fixed.. http://jsfiddle.net/yjZVd/6/

Upvotes: 4

Related Questions