Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8206455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:40:37+00:00 2026-06-07T08:40:37+00:00

I am attempting to add some functions to backbone so that I can communicate

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T08:40:38+00:00Added an answer on June 7, 2026 at 8:40 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to add some entities that I have created. When I try
I am attempting add some tests to an existing QT GUI application using QTest.
I have am attempting to add some input fields to a jquery ui dialog
I'm attempting to write an AJAX control extender that can modify an AJAX Control
So I am really new to VIM but was attempting to add some plugin's
I've been attempting to modify some code that I was using previously with jQuery
I'm attempting to add some text to the directions results the Google Maps API
I'm attempting to add the ability to drag and drop to reorder some boxes
I have a class that inherits from a dictionary in order to add some
I've done some work with Scriptaculous, and now I'm dipping my feet in to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.