Book Image

Node.js By Example

Book Image

Node.js By Example

Overview of this book

Table of Contents (18 chapters)
Node.js By Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a model


We will probably have several models and all of them will share the same methods. Normally, the models make HTTP requests to the server and get data. So, this is something that we need to abstract. Thankfully, Ractive.js makes it possible for you to extend components. Here is the code for the models/Version.js file:

var Base = require('./Base');
module.exports = Base.extend({
  data: {
    url: '/api/version'
  }
});

We have models/Base.js, the file that will contain these common functions. It will be a base class that we will later inherit.

var ajax = require('../lib/Ajax');
module.exports = Ractive.extend({
  data: {
    value: null,
    url: ''
  },
  fetch: function() {
    var self = this;
    ajax.request({
      url: self.get('url'),
      json: true
    })
    .done(function(result) {
      self.set('value', result);
    })
    .fail(function(xhr) {
      self.fire('Error fetching ' + self.get('url'))
    });
    return this;
  },
  bindComponent: function(component...