Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Latest commit

 

History

History
47 lines (37 loc) · 956 Bytes

extending-modules.md

File metadata and controls

47 lines (37 loc) · 956 Bytes

Extending

It is common in an application to have module's that share behavior, but are slightly different. In this case you can extend from modules you have already defined (working example).

var Apple = fruitmachine.define({
  name: 'apple',
  template: function(){ return ''; },
  initialize: function() {
    alert('we are similar');
  },
  setup: function() {
    alert('but i am an apple');
  }
});

var Pear = fruitmachine.define(
  Apple.extend({
    name: 'pear',
    template: function(){ return ''; },
    setup: function() {
      alert('and i am an pear');
    }
  })
);

var apple = new Apple();
//=> alert - 'we are similar'

var pear = new Pear();
//=> alert - 'we are similar'

apple
  .render()
  .appendTo(document.body);

pear
  .render()
  .appendTo(document.body);

apple.setup();
//=> alert - 'but i am an apple'

pear.setup();
//=> alert - 'but i am an pear'