Как использовать jQuery $ .extend (obj1, obj2)

0

Я пытаюсь создать класс кнопки, который расширяет класс abstractcomponent с помощью $.extend(), но функции в abstractcomponent arent доступны, когда im конструируют кнопку.

Конкретная ошибка получения im: Uncaught TypeError: Object [object Object] не имеет метода 'setOptions'

var Button = {};
var abstract = new AbstractComponent;
$.extend(Button,abstract);
//debugger;
//this.setOptions is available here
Button = function(options) {
    'use strict';
    var defaultOptions = {
        templateName: '#button-tmpl',
        title: "Label goes here",
        type: "primary",
        size: "medium",
        disabled: null,
        autosave: null,
        href: null,
        onclick: null
    };
//debugger
//this.setOptions is not available here
    this.setOptions(options, defaultOptions);
    this.checkRequiredKeys('title');
    return this;
};
Button.prototype.updateOptions = function() {
    var options = this.options;
    if (options.href === null) {
        options.href = 'javascript:;';
    }
    if (options.disabled === null) {
        options.disabled = 'disabled';
    }
    if (options.autosave === true) {
        options.autosave = 'ping-autosave';
    }
};

AbstractComponent.js

var AbstractComponent = function() {
    console.log('this will be the constructor for elements extending this class');
};
AbstractComponent.prototype.show = function() {
    this.render();
};
AbstractComponent.prototype.close = function() {
    // stop listeners and remove this component
    this.stopListening();
    this.remove();
};
AbstractComponent.prototype.getTemplateName = function() {
    return this.options.templateName;
};    
AbstractComponent.prototype.checkRequiredKeys = function() {
    var errors = new Array();
    if (typeof this.getTemplateName() === "undefined") {
        errors.push('templateName');
    }
    for (var i = 0; i < arguments.length; i++) {
        if (!this.options.hasOwnProperty(arguments[i])) {
            errors.push(arguments[i]);
        }
    }
    if (errors.length > 0) {
        throw new Exception("Required property(s) not found:" + errors.join(', ') + " in " + this.toString());
    }
};

AbstractComponent.prototype.getElement = function() {
    'use strict';
    if(!this.options.updated) {
        this.updateOptions();
    }
    return new AbstractView(this.options).render().$el;
};


AbstractComponent.prototype.updateOptions = function() {
    this.options.updated = true;
    return true;
};

AbstractComponent.prototype.getHtml = function() {
    return this.getElement().html();
};

AbstractComponent.prototype.setOptions = function(options, defaultOptions) {
    this.options = _.defaults(options, defaultOptions);
};

AbstractComponent.prototype.toString = function() {
    return "Component" + this.getTemplateName() + "[id=" + this.options.id + "]";
};
Теги:
extends

1 ответ

3
Лучший ответ

Расширение jQuery - это перемещение свойств от одного (или более) объекта (ов) к другому объекту.

$.extend({}, {
   foo: 10,
   bar: 20
});

Вы должны использовать прототипное наследование isntead

function Button(options) {
    'use strict';
    var defaultOptions = {
        templateName: '#button-tmpl',
        title: "Label goes here",
        type: "primary",
        size: "medium",
        disabled: null,
        autosave: null,
        href: null,
        onclick: null
    };
//debugger
//this.setOptions is not available here
    this.setOptions(options, defaultOptions);
    this.checkRequiredKeys('title');
    return this;
};
Button.prototype = new AbstractComponent;
  • 0
    Потрясающие! Спасибо!

Ещё вопросы

Сообщество Overcoder
Наверх
Меню