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

  • Home
  • SEARCH
  • 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 8989559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:17:26+00:00 2026-06-15T22:17:26+00:00

I am attempting to create a generic Container for my viewModels so common methods

  • 0

I am attempting to create a generic Container for my viewModels so common methods can be applied a variety of objects without specific knowledge ot the viewModel. The container and contained object would look like this:

var containedViewModel = function() {
  var self = this;
  self.id = ko.observable();
    ...
  self.doSomething = function() {
  alert('here');
  };

}

var ContainerModel = function(cRoot, cModel, cName) {
  var self = this;

  self.rootModel  = cRoot;                       // Root view model
  self.viewName   = cName;                       // viewModel container name
  self.refModel   = cModel;                      // viewModel reference
  self.viewModel  = ko.observable();             // Single view model
  self.viewModels = ko.observableArray();        // Array of view models

  self.init = function(rootModel) {
    self.viewModel = new self.refModel();
  } 
  self.doSomething = function() {
    self.rootModel.doSomeThing();    // This works
    self.refModel.doSomeThing();     // This does not work
    self.viewModel.doSomeThing();    // This does not work as well
  } 

}

And the container would be created with a call like:

var ParnentModel = function() {
  var self = this;
  self.id = ko.observable();
    ...
  self.container = new ContainerModel(self, containedViewModel, 'modelName');
    ...
  self.doSomething = function() {
  alert('here');
  };
};

In this example the rootModel function access works fine because the actual viewmodel is created and passed to the container. Using ‘new self.refModel()’ and ‘self.rootModel.doSomeThing()’ appear to work as expected. When I attempt to use ‘self.viewModel.doSomeThing();’ knockout complains that it is not a function.

Is it possible to access a viewModels functions by reference to the viewModel.

Any help would be appreciated.

  • 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-15T22:17:27+00:00Added an answer on June 15, 2026 at 10:17 pm

    You’re almost there. See my comments inside the code.

    var containedViewModel = function() {
      var self = this;
      self.id = ko.observable();
      self.doSomething = function() {
        alert('contained');
      };
      // I would prefer to have return self here
    };
    
    var ContainerModel = function(cRoot, cModel, cName) {
      var self = this;
    
      self.rootModel  = cRoot;                       // Root view model
      self.viewName   = cName;                       // viewModel container name
      self.refModel   = cModel;                      // viewModel reference
      self.viewModel  = ko.observable();             // Single view model
      self.viewModels = ko.observableArray();        // Array of view models
    
      self.init = function(rootModel) {
        // you meant this, right?
        self.viewModel(new self.refModel());
      }; 
      self.doSomething = function() {
        self.rootModel.doSomething();    // This works
        //self.refModel.doSomeThing();     // This does not work
        // need to unwrap the value, fixed typo
        self.viewModel().doSomething();    // This does not work as well
      };
    };
    
    var ParnentModel = function() {
      var self = this;
      self.id = ko.observable();
      self.container = new ContainerModel(self, containedViewModel, 'modelName');
      // missing call to init
      self.container.init();
      self.doSomething = function() {
          alert('parent');
      };
    };
    
    // execution
    var p = new ParnentModel();
    p.container.doSomething();
    

    http://jsbin.com/arezew/1/edit

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

Sidebar

Related Questions

I am attempting to create an Expression that will invoke a specific generic overloaded
I'm attempting to create a generic controller, ie: public class MyController<T> : Controller where
I attempting to create an 800x500 div with four overlapping divs inside the container
I am new to LINQ to SQL and attempting to create a generic Data
I'm attempting to create a generic game of blackjack through javascript. The game starts
In attempting to create a thread-safe container class from scratch, I've run into the
I am attempting to create a generic mapping function that takes the values of
So I am attempting to create a class which uses a generic which extends
I'm attempting to make a generic routine within my program that will instantiate objects
While attempting to create a replacement tablespace for USER, I accidentally created a datafile

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.