Backbone Template

Standard fiddle

by Steven Senkus

HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone-min.js"></script>
<div id="container"></div>
<div id="timeUpdated"></div>

JavaScript

function getArrivals() {
      //$('#container').empty();
      // can we query multiple locids??  then do this:
      var locIDs = ['2580'];
      var locIDsString = locIDs.join();
      //end
      var trimetURL = 'http://developer.trimet.org/ws/V1/arrivals/locIDs/' + locIDs[0] + '/appID/5E48DCD7031297B7CBF2F37FD?json=true';
      var result;
      $.ajax({
          async: false,
          type: "GET",
          url: trimetURL,
          dataType: 'json',
          success: function (data) {
              result = (JSON.stringify(data.resultSet));

          },
          error: function (e) {
              console.log('error', e);
          },
          complete: function () {

          }
      });
      return result;
  }

  var Person = Backbone.Model.extend({
      defaults: {
          name: 'default name',
          age: 99,
          job: 'default job'

      },
      validate: function (attributes) {
          console.log('attr', attributes);
          if (attributes.age < 0) {
              return "VALIDATION ERROR: You can't be negative years old";
          }
      },
      initialize: function () {
          this.on('change:age', function (model) {
              alert('age change:');
          
          });


      }

  });


  var homeView = Backbone.View.extend({
      el: $('#container'),
      initialize: function () {
          this.render();
      },
      render: function () {

          var data = getArrivals();
          //          console.log('data', data);



          this.$el.html('hello bb');
      }
  });
Backbone.sync = function(method, model, options) {


};




  var h = new homeView();
  var person = new Person();
  person.set({
      name: 'Steve',
      age: 29
  });
  person.set({
      job: 'toilet scrubber'
  });

  person.on("invalid", function (model, error) {
      // We have received an error, log it, alert it or forget it :)
      alert(error);
  });

  person.save(
      'age', -100);
  console.log(person.toJSON(),...