diff --git a/javascripts/app.js b/javascripts/app.js index 319f839..d7398ab 100644 --- a/javascripts/app.js +++ b/javascripts/app.js @@ -1,11 +1,15 @@ -define(['jquery', - 'underscore', - 'jquerymobile', +define(['jquerymobile', 'backbone', - 'mustache'], -function($, _, a, Backbone, Mustache) { + 'mustache', + 'collections/lines', + 'views/line_list'], +function(a, Backbone, Mustache, LinesCollection, LineList) { return { initialize: function() { + lines = new LinesCollection; + lineList = new LineList({ collection: lines }); + + lines.fetch(); } }; }); diff --git a/javascripts/collections/lines.js b/javascripts/collections/lines.js index 9e3f152..b27e7bf 100644 --- a/javascripts/collections/lines.js +++ b/javascripts/collections/lines.js @@ -1,11 +1,23 @@ -require(['backbone', 'models/line'], function(Backbone, Line) { +define(['backbone', 'models/line'], function(Backbone, Line) { + + var MySync = function (method, collection, options) { +    options.timeout = 10000; // required, or the application won't pick up on 404 responses +    options.dataType = "jsonp"; +    return Backbone.sync(method, collection, options); + }; var LinesCollection = Backbone.Collection.extend({ url: "http://api.tubeupdates.com/?method=get.status", + model: Line, + + sync: MySync, + parse: function(data) { return data.response.lines; } }); + return LinesCollection; + }); diff --git a/javascripts/models/line.js b/javascripts/models/line.js index 82a7da7..ea80ca7 100644 --- a/javascripts/models/line.js +++ b/javascripts/models/line.js @@ -1,5 +1,9 @@ -require(['backbone'], function(Backbone) { +define(['backbone'], function(Backbone) { + var Line = Backbone.Model.extend({ }); + + return Line; + }); diff --git a/javascripts/templates/line.mustache b/javascripts/templates/line.mustache index cbaa72a..6cdfcf6 100644 --- a/javascripts/templates/line.mustache +++ b/javascripts/templates/line.mustache @@ -1 +1 @@ -
  • {{ name }} ({{ status }})
  • +
  • {{{ name }}} ({{ status }})
  • diff --git a/javascripts/views/line_list.js b/javascripts/views/line_list.js index c4b29f6..74ea221 100644 --- a/javascripts/views/line_list.js +++ b/javascripts/views/line_list.js @@ -1,15 +1,32 @@ define(['backbone', 'text', + 'mustache', 'models/line', 'collections/lines', 'text!templates/line.mustache'], function(Backbone, Text, + Mustache, Line, LineCollection, lineTemplate) { var LineList = Backbone.View.extend({ + initialize: function() { + var view = this; + this.collection.on("reset", function() { + view.render(); + }); + }, + + render: function() { + var html = ""; + this.collection.models.forEach(function(model) { + html += Mustache.render(lineTemplate, model.toJSON()); + }); + this.$el.html(html).listview("refresh"); + }, + el: '#line-list' });