JSFiddle - React, Tailwind, and code Playground

by Eric Howe

HTML

<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
<script src="https://raw.github.com/jeromegn/Backbone.localStorage/master/backbone.localStorage-min.js"></script>
 <button id="add-friend">Add Friend</button>

<ul id="friends-list"> </ul>

CSS

.nice {
    
    background-color: grey; 
    
    
}

JavaScript

var Friend = Backbone.Model.extend({
    name: null
});

var Friends = Backbone.Collection.extend({
    model: Friend,
    localStorage: new Backbone.LocalStorage("friends-list")
});

var FriendView = Backbone.View.extend({
    tagName: 'li',
    className: 'nice',
    initialize: function() {
        _.bindAll(this, 'render');
        this.model.bind('change', this.render);
    },
    render: function(model) {
        this.$el.html('<span class="friend" style="color:pink">' + this.model.get('name') + '</span>');
        return this;
    }
});

window.AppView = Backbone.View.extend({
    el: $("body"),
    events: {
        "click #add-friend": "showPrompt"
    },

    initialize: function() {
        _.bindAll(this, 'render', 'showPrompt');
        this.collection = new Friends();
        this.collection.bind('add', this.render);
        this.collection.bind('reset', this.render);
        this.collection.fetch();
    },

    showPrompt: function() {
        var friend_name = prompt("Who is your friend?");
        var friend_model = new Friend({
            name: friend_name
        });
        //Add a new friend model to our friend collection
        this.collection.add(friend_model);
        friend_model.save();
    },

    render: function() {
        var $list = this.$('#friends-list');
        $list.empty();
        this.collection.each(function(m) {
            var newFriend = new FriendView({ model: m });
            $list.append(newFriend.render().el);
        });
        $list.sortable();
        return this; 
    }
});
var appview = new AppView();