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) ...

Get Node.js By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.