Parris
Parris

Reputation: 18408

Adding more functions to Backbone Models

I am attempting to add some functions to backbone so that I can communicate with mongodb. Now I know this won't work client side; however, I do like backbone's functionality for server side model logic as well. I noticed that I would be doing a bunch of repeat work if I kept adding the same functionality for each model so decided to create a "app_model" file to extend backbone when I'm server side. I also don't want to override the standard Backbone functions because they will be useful client side.

So let's take this user class for instance:

var Backbone = require('./app_model');

var User = Backbone.Model.extend({
    name : "users",
    defaults: function() {
        return {
            username: "default",
            role: 2,
            created: new Date(),
            updated: new Date(),
            logged: new Date()
        };
    },
    idAttribute: "username",
    /**
     * A predefined listing of user roles
     */
    userRoles: [
        "admin",  //0
        "author", //1
        "user"    //2
    ],
    initialize: function() {
        if(!!app) {
           this.svrInit();
        }
    }
});

module.exports = User;

And I want to append functions onto backbone by using my "app_model.js" file, which looks something like this currently:

var Backbone = require('backbone'),
  Deferred = require('Deferred'),
  when = Deferred.when;

Backbone.Model.prototype.svrInit = function() {
    //TODO: perhaps the code below should be made static some how so we don't have a bunch of instances of collection
    var model = this;
    if(!!app.db){
        app.db.collection(this.name,function(err,collection){
            model.collection = collection;
        });
    }
};

Backbone.Model.prototype.svrSave = function() {
    var model = this.toJSON();
    var dfd = new Deferred();
    this.collection.insert(model, {safe:true}, function(err, result){
        dfd.resolve();
    });
    return dfd;
};

Backbone.Model.prototype.svrFind = function(options) {
    var model = this.toJSON();
    var dfd = new Deferred();
    this.collection.find(options, {safe:true}, function(err, result){
        dfd.resolve();
    });
    return dfd;
};

module.exports = Backbone;

I ran my tests when I abstracted this out and it seemed to work alright. Is there a better way to do any of this? Any pit falls? I am using the global "app" variable, is that bad? If so what are some ways around it? I do find it ugly that I had to put this.svrInit() inside the init function at the model level is there anyway to automatically make that happen after creation?

Upvotes: 2

Views: 3360

Answers (2)

Mark Wilbur
Mark Wilbur

Reputation: 2915

I'm working on a fairly large code-base with 4-5 levels of inheritance in the views. This is the pattern I'm using:

var BaseView = Backbone.Model.extend({
  somefunc: function() {
    //contents
  },

  otherfunc: function(a,b,c) {
    //contents
  },
  //...
});


var User = BaseView.extend({
  // things in user view can now access somefunc and otherfunc
});

Here's a quick example in a jsfiddle (note the doSearch function being inherited)

Upvotes: 1

rfunduk
rfunduk

Reputation: 30442

So I've been thinking about this question for a couple days and I the cleanest thing I've come up with is something like this:

var MyModel = function( attributes, options ) {
  Backbone.Model.apply( this, arguments );
  this.specialInitializer();
};

MyModel.extend = Backbone.Model.extend;

_.extend( MyModel.prototype, Backbone.Model.prototype, {
  specialInitializer: function() {
    // called after the users 'initialize'
    console.log("MyModel initialized.", this);
  },
  otherNewMethod: function() {
    // this is just like any other instance method,
    // just as if Backbone.Model implemented it
  }
} );

So what this does is basically make an entirely new 'kind' of Backbone.Model. One which also calls specialInitializer. If you look at the backbone source just after the constructor definition for Backbone.Model you'll see this is a similar strategy.

  • Construct the instance.
  • Call an initializer the implementor is supposed to define.
  • Extend the prototype with functionality (in their case Backbone.Events, in ours, Backbone.Model).

Your new initializer can of course call whatever else it needs, etc.

As for your other questions about the static collection stuff and global app variable, I'm afraid I don't follow exactly what is going on there since I don't see a definition for app and don't know what you're using the collection for.

Here's a fiddle that demonstrates this with some extra logging and such.

Upvotes: 5

Related Questions