Slide 1

Slide 1 text

Backbone.Model A refresher, and hence a beginner course

Slide 2

Slide 2 text

Talk Plan 1. Backbone.Model, in general. 2. [Demo] Patterns of Backbone. 3. [Demo] Limitations of Model & Collection. < 15 min < 15 min

Slide 3

Slide 3 text

Background • Backbone is one of the many JavaScript libraries we are using. • Introduced by the US team and has survived > 4 years in our company. • We don’t always get to maintain what we want. • Backbone.Model is one of the main modules in Backbone MVP stack.

Slide 4

Slide 4 text

Purpose • We need to tame Backbone, understand its deficiencies and strengths. • Don’t work against it, i.e. don’t overwrite their functions. • Learn to leverage on best Backbone patterns.

Slide 5

Slide 5 text

Magic-o-meter Backbone POJO Ember Angular React.js

Slide 6

Slide 6 text

Characteristics of a model 1. Extensible using extend() 2. Mutable using get() and set() 3. Observable using V.listenTo() 4. Non-nested 5. Non-composite ID attribute 6. Free HTTP mapping

Slide 7

Slide 7 text

1. Extensible var  Customer  =  Backbone.Model.extend({
      defaults:  {
            noOfCats:  0
      }
 });   var  larry  =  new  Customer({
      name:  'Larry'
 });   How many cats does Larry have?

Slide 8

Slide 8 text

0

Slide 9

Slide 9 text

1. Extensible var  Customer  =  Backbone.Model.extend({
      defaults:  {
            noOfCats:  0
      }
 });   var  larry  =  new  Customer({
      name:  'Larry',
      noOfCats:  4
 });   How many cats does Larry have?

Slide 10

Slide 10 text

4

Slide 11

Slide 11 text

2. Mutability var  larry  =  new  Customer({
      name:  'Larry',
      noOfCats:  4
 });   larry.set('noOfCats',  2);   larry.get(‘noOfCats');   How many cats does Larry now have?

Slide 12

Slide 12 text

2

Slide 13

Slide 13 text

3. Observable var  larry  =  new  Customer({
      name:  'Larry',
      noOfCats:  4
 });   larry.on('change:noOfCats',  function()  {
      //  Something  happens
      //  Mice  gets  nervous?
 });   Highly recommended to use .listenTo() from the View instead.

Slide 14

Slide 14 text

4. Non-nested var  larry  =  new  Customer({
      name:  {
            firstName:  'Larry',
            lastName:  'King'
      },
      noOfCats:  4
 });   Object within object is not supported. You cannot observe the change of name.firstName. You can only observe the change of name.

Slide 15

Slide 15 text

5. Non-composite ID • By default the idAttribute is ‘id’. (Can overwrite.) • id should correspond with ID in the backend • This is not possible: var  larry  =  new  Customer({
      id:  {
            network:  1,
            user_id:  1802
      },
      name:  'Larry'
 }); God kills a kitten once you attempt this.

Slide 16

Slide 16 text

6. HTTP mapping CRUD action model.isNew() model. HTTP Retrieving FALSE .fetch() GET Creating TRUE .save() POST Updating FALSE .save() PUT Deleting FALSE .destroy() DELETE

Slide 17

Slide 17 text

Demo With the information you learnt, let’s try it out! git clone https://github.com/kahwee/backbone-model-demo.git

Slide 18

Slide 18 text

Links • Annotated source code
 http://backbonejs.org/docs/backbone.html • Issues and direction
 https://github.com/jashkenas/backbone • Videos and conference
 http://backboneconf.com/