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 8651171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:59:49+00:00 2026-06-12T13:59:49+00:00

var Model = function(client, collection) { this.client = client; this.collection = collection; }; Model.prototype

  • 0
var Model = function(client, collection) {
  this.client = client;
  this.collection = collection;
};

Model.prototype = {
  constructor: Model,
  getClient: function(callback) {
    this.client.open(callback);
  },
  getCollection: function(callback) {
    var self = this;

    this.getClient(function(error, client) {
      client.collection(self.collection, callback);
    });
  },
  extend: function(key, fn) {
    var self = this;

    this[key] = function() {
      fn.call(self); // A
    };
  }
};

What I want to achieve is that I can “extend” the functionality of the model.

var userModel = new Model(client, 'users');
userModel.extend('create', function(data, callback) {
  this.getCollection(function(error, collection) {
    collection.insert(data, { safe: true }, function(error, doc) {
      callback.call(doc);
    });
  });
});

userModel.create({ fullName: 'Thomas Anderson' }, function() {
  console.log(this); // { _id: '123456789012345678901234', fullName: 'Thomas Anderson' }
});

Someway at A, I have to do parameter passing, the parameter count of the custom “create” function (data and callback) are variable.

Is this possible and if so how?

  • 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-12T13:59:51+00:00Added an answer on June 12, 2026 at 1:59 pm

    Yep! You want to use the .apply method over the .call method. Both apply context, but the .apply method takes an array for arguments–or an arguments collection itself!

    extend: function(key, fn) {
        var self = this;
    
        this[key] = function() {
          fn.apply(self,arguments); // the special arguments variable is array-like
        };
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the following as an example: var Case = Backbone.Model.extend({ initialize: function(){ this.bind(error, function(model,
I have code like this in my view model: function ChatListViewModel(chats) { var self
I have the following situation: var Task = Backbone.Model.extend({ initialize: function() { }, save:
var JavascriptHelper = Backbone.Model.extend(JavascriptHelper, {}, // never initialized as an instance { myFn: function()
I have this model var Item = Backbone.Model.extend({ url: 'http://localhost/InterprisePOS/Product/loaditembycategory/Event Materials' }); var onSuccess
If i have this model: var myParentModel = Backbone.Model.extend({ defaults:{ parent1: null, parent2: null}
This is my front-end code (using fetch ) var MyModel = Backbone.Model.extend(); var MyCollection
So I have a view model like so: var viewModel = function() { var
var Modal = function (content, options) { this.options = options this.$element = $(content) .delegate('[data-dismiss=modal]',
I have fit a simple bi-variate VAR model to this data set and I

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.