Archive for the ‘Tech’ Category

A Simple Backbone.js App

Sunday, April 28th, 2013

Testing the CodeColorer plugin for WordPress. Here’s a snippet I’ve written for a simple model Page() with a collection PageList() and their corresponding views PageContentView() and PageListView().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
var Page = Backbone.Model.extend({
    defaults: {
        name: '',
        title: '',
        description: '',
        keywords: '',
        content: []
    },
    validate: function(attrs, options) {
        var name = attrs.name;
        if (name.match(/\s+/g) != null) {
            return "invalid name attribute";
        }
    },
    idAttribute: '_id',
    initialize: function () {
        this.on("change", function (model) {});
    }
});

var PageList = Backbone.Collection.extend({
    model: Page,
    url: "/api/page",
    idAttribute: '_id',
    initialize: function () {
        _.bindAll(this, 'retrieve');
        this.retrieve();
    },
    retrieve: function () {
        var self = this;
        this.fetch({
            success: function (collection, response, options) {
                self.reset();
                self.add(response);
                self.trigger("change");
            },
            error: function (collection, xhr, options) {
            }
        });
    }
});

var PageContentView = Backbone.View.extend({
    tagName: "tr",
    initialize: function () {
        _.bindAll(this, 'render');
        this.model.bind('change', this.render);
    },
    render: function () {
        var pageObj = {
            id: this.model.get("_id"),
            name: this.model.get("name"),
            title: this.model.get("title"),
            description: this.model.get("description"),
            keywords: this.model.get("keywords"),
            content: this.model.get("content")
        };
        this.$el.append(_.template($("#list-page-template").html(), pageObj));
        return this;
    }
});


var PageListView = Backbone.View.extend({
    el: '#page-list',
    initialize: function() {
         _.bindAll(this, 'render', 'renderPageRow', 'renderTemplate',
              'addPage','editPage','savePage','cancelEditPage','deletePage');
        this.collection = new PageList();
        this.collection.bind('change', this.render, this);
        this.collection.bind('remove', this.render, this);
    },


    events: {
        "click .add-page-button": "addPage",
        "click .edit-page-button": "editPage",
        "click .save-page-edit": "savePage",
        "click .cancel-page-edit": "cancelEditPage",
        "click .delete-page-button": "deletePage"
    },

    render: function () {
        var self = this;
        this.$el.html("");
        this.$el.append(this.renderTemplate("#add-page-template"));
        _(this.collection.models).each(function (item) {
            self.renderPageRow(item);
        }, this);
        return this;
    },

    renderTemplate: function (name, obj) {
        return _.template($(name).html(), obj);
    },

    renderPageRow: function (item) {
        var pageItem = new PageContentView({model: item});
        this.$el.append(pageItem.render().el);
    },

    addPage: function (e) {
        e.preventDefault();
        var newPage = new Page({
            name: $('#page-name').val(),
            title: $('#page-title').val()
        });
        this.collection.add(newPage, {merge: true, at: 0});
        newPage.save({}, {
            success: function () {
            },
            error: function () {
            }
        });
        return false;
    },

    editPage: function (e) {
        e.preventDefault();
        var model = this.collection.get(e.target.value);
        var el = $(e.target).closest('tr');
        el.empty();

        var content = model.get("content");
        var selectedIDs = [];

        console.debug("content-attribute:", typeof content);
        if (typeof content === "string") {
            selectedIDs = content.split("|");
            console.debug("selectedIDs split: ", selectedIDs);
        } else if (typeof content === "object") {
            selectedIDs = _.pluck(model.get("content"),"_id");
            console.debug("selectedIDs plucked: ", selectedIDs);
        }

        var textList = [];
        _.each(textListView.collection.models, function(item){
            var menuItem = {
                _id: item.get("_id"),
                name: item.get("name")
            };
            textList.push(menuItem);
        });
        textList.sort(function(a,b){
            if (a.name == b.name) return 0;
            if (a.name < b.name) return -1;
            else return 1;
        });

        var pageObj = {
            id: model.get("_id"),
            name: model.get("name"),
            title: model.get("title"),
            description: model.get("description"),
            keywords: model.get("keywords"),
            content: selectedIDs,
            textList: textList
        };

        var template = this.renderTemplate("#edit-page-template",pageObj);
        el.append($(template));
        return false;
    },

    savePage: function (e) {
        e.preventDefault();
        var model = this.collection.get(e.target.value);
        var id = model.get("_id");

        var pageObj = {
            title: $('#title-'+id).val(),
            description: $('#desc-'+id).val(),
            keywords: $('#keywords-'+id).val()
        };

        if (model) {
            model.set(pageObj);
        }

        var selectedIDs = [];
        $("#content-"+id+" option:selected")
            .each(function(){
                selectedIDs.push($(this).val());
            });

        if (selectedIDs.length)
            model.set("content", selectedIDs.join("|"));
        else
            model.set("content", null);

        model.save({},{
            success: function() {},
            error: function() {}
        });
        return false;
    },

    cancelEditPage: function (e) {
        e.preventDefault();
        this.render();
        return false;
    },

    deletePage: function (e) {
        e.preventDefault();
        var model = this.collection.get(e.target.value);
        model.destroy({
            success: function(model, response, options) {
                console.debug("deleted", model);
            },
            error: function(model, xhr, options) {
                console.debug("error deleting");
            }
        });
        return false;
    }
});

JavaOne Conference Notes

Sunday, September 26th, 2010

Picked 3 of my favorite topics from the recently concluded JavaOne conference in San Francisco. Link to PDF (5.5mb).

Jessica Sanders for Sony’s Make.Believe ad campaign

Saturday, February 20th, 2010

Found this clip on the PlayStation Network today as I was browsing for free videos to download. ┬áIt’s part of Sony’s “Make.Believe” ad campaign featuring real people using their products.

Sorry, No More Exotic Location Shoots …

Wednesday, February 17th, 2010
YouTube Preview Image

Facebook Launches HD Video

Monday, December 15th, 2008

Cool beans! Facebook now supports HD video sharing up to 720p (1280×720).
Image quality can compete with Vimeo too.
Here are a few from my profile:

San Francisco Holiday Scene

Parol Lantern Parade, San Francisco ’08

Don Edwards Wildlife Refuge

Thing 1 and Thing 2

-kyo-