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 (attr) {
          console.log('attr', attr);
          if (attr.age < 0 || attr.name == "Steve") {
              return "VALIDATION ERROR: You can't be negative years old";
          }
      },
      initialize: function () {
          //this.on('change:age', function (model) {});
        this.validate();

      }

  });


  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');
      }
  });

  var h = new homeView();
  var person = new Person();
  person.set({
      name: 'Steve',
      age: 29
  });
  person.set({
      age: -100
  });
  
  person.bind("error", function (model, error) {
      // We have received an error, log it, alert it or forget it :)
      alert(error);
  });

  console.log(person.toJSON(), person.attributes);