2

有什么办法可以让我拥有一个扩展通过显示模块模式创建的对象的 Javascript 类?我尝试了以下代码,但有没有实现同样的目标?

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
    };
};
4

1 回答 1

2

你可以jQuery.extend用来实现这种行为。

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();

http://jsfiddle.net/GEGNM/上进行测试

于 2012-12-22T01:22:10.157 回答