2

Is there any way I can do have a Javascript class that extends an object that was created through the revealing module pattern? I tried the following code, but is there away to achieve the same thing?

sv.MergeQuestionViewModel = function () {
    this = sv.QuestionDetailViewModal();
    this.init($("#mergeQuestionModel"));
};  

sv.QuestionDetailViewModal = function () {
    var $el,
        self = this,
        _question = ko.observable(),
        _status = new sv.Status();

    var _init = function (el) {
        $el = el;
        $el.modal({
            show: false,
            backdrop: "static"
        });
    };

    var _show = function () {
        $el.modal('show');
    };

    var _render = function (item) {
        _question(new sv.QuestionViewModel(item));
        _show();
    };

    var _reset = function () {
        _question(null);
        _status.clear();
    };

    var _close = function () {
        $el.modal('hide');
        _reset();
    };

    return {
        init: _init,
        show: _show,
        render: _render,
        reset: _reset,
        close: _close
    };
};
Abe
  • 6,386
  • 12
  • 46
  • 75
  • possible duplicate of [How to implement inheritance in JS Revealing prototype pattern?](http://stackoverflow.com/questions/9248655/how-to-implement-inheritance-in-js-revealing-prototype-pattern) – Bergi Dec 23 '12 at 11:35
  • For what purpose are you using the `this` keywords in your code? – Bergi Dec 23 '12 at 11:36

1 Answers1

2

You could use jQuery.extend to achive this behaviour.

sv.MergeQuestionViewModel = function () {
    $.extend(this, sv.QuestionDetailViewModal);

    this.init($("#mergeQuestionModel"));
};

sv.QuestionDetailViewModal = (function () {
 var el,

 _init = function($el) {
    el = $el;
    console.log('init', el);
 },

 _render = function() {
    console.log('render', el);
 };

 return {
   init : _init,
   render : _render
 };
}());

var view = new sv.MergeQuestionViewModel();
view.render();

Test it on http://jsfiddle.net/GEGNM/

nekman
  • 1,919
  • 2
  • 15
  • 26
  • Sure, I realize the code does not work, but I also wanted to demonstrate what I was going for. My question is there alternative way to achieve this. Thanks! – Abe Dec 23 '12 at 04:51
  • I was 99.9% sure you knew that, but I just wanted to assure it. I have updated my answer. – nekman Dec 23 '12 at 11:23