2015-10-13 21 views
6

मैं कुछ अतिरिक्त सुविधाओं को प्रदान करने के लिए Handsontable के आसपास एक रैपर बनाने की कोशिश कर रहा हूं। मैंने निम्नलिखित करने का प्रयास किया है और हालांकि कन्स्ट्रक्टर काम करता है, लोडडाटा फ़ंक्शन ओवरराइड नहीं लग रहा है। कोई सलाह?विस्तारित हैंडसेटोबल

मैंने क्रोम 45.0.2454.101 मीटर में इसका परीक्षण किया है।

"use strict"; 

class CustomHandsontable extends Handsontable { 

    constructor(container, options) { 
     console.log("in constructor"); 
     super(container, options); 
    } 

    loadData(data) { 
     console.log("load data"); 
     super.loadData(data); 
    } 
} 

संपादित करें: पढ़ने कि क्रोम अभी तक पूरी तरह ECMA6 का समर्थन नहीं करता के बाद, मैं निम्नलिखित की कोशिश की है, लेकिन यह समझ नहीं कर सकते कि माता-पिता loadData विधि कॉल करने के।

function CustomHandsontable (container, options) { 
    console.log("constructor"); 

    this.loadData = function(data) { 
     console.log("loadData"); 
     // TODO how do you call the parent load data function? 
    } 
}; 

CustomHandsontable.prototype = Object.create(Handsontable.prototype); 
CustomHandsontable.prototype.constructor = CustomHandsontable; 

संपादित करें:

Uncaught TypeError: Cannot read property 'call' of undefined 

Babel code:

"use strict"; 

var _createClass = (function() { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); 

var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; 

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } 

var Hot = (function (_Handsontable) { 
    _inherits(Hot, _Handsontable); 

    function Hot(localOptions, container, options) { 
     _classCallCheck(this, Hot); 

     console.log("in constructor"); 
     _get(Object.getPrototypeOf(Hot.prototype), "constructor", this).call(this, container, options); 
    } 

    _createClass(Hot, [{ 
     key: "loadData", 
     value: function loadData(data) { 
      console.log("load data"); 
      _get(Object.getPrototypeOf(Hot.prototype), "loadData", this).call(this, data); 
     } 
    }]); 

    return Hot; 
})(Handsontable); 
+0

क्या आपने अपने ईएस 6 कोड को ईएस 5 में ट्रांसफर करने के लिए बेबेल का उपयोग करने का प्रयास किया है? आजकल कोड लिखने का यह एक मानक तरीका है। यह सलाह दी जाएगी क्योंकि हैंडसोंटेबल ने पहले से ही ES6 और ES7 कोडिंग रूपांतरण को अपनाया है। – ZekeDroid

+0

@ZekeDroid: बेबेल पर टिप के लिए धन्यवाद। मैंने कोशिश की, लेकिन एक त्रुटि प्राप्त करें (मेरी अद्यतन पोस्ट देखें)। –

+0

हाँ मुझे यकीन नहीं है कि इसका क्या करना है। बस उत्सुक है, क्या आपको वास्तव में हैंडोंटेबल का विस्तार करने की आवश्यकता है? आप अपने निर्माण को कस्टम बिल्ड बनाने के तरीके पर पढ़ सकते हैं, अन्यथा, मैंने पाया है कि लगभग कोई मामला नहीं है जहां सामान्य डेटा मैनिप्लेशंस और जेएस उनके उद्देश्य की सेवा नहीं करते हैं – ZekeDroid

उत्तर

2

जवाब के बाद @ZekeDroids टिप कोलाहल उपयोग करने के लिए, मैं निम्नलिखित त्रुटि जब इसकी सुपर वर्गों loadData फ़ंक्शन को कॉल करने का प्रयास कर पाने मेरा खुद का सवाल ...

मैं विरासत टी प्राप्त करने में सक्षम नहीं था ओ काम, लेकिन संरचना का उपयोग करके समाधान मिला:

function Hot(container, options, customOptions) { 
    var _instance = this; 

    // create a hot instance 
    var hot = new Handsontable(container, options); 
    hot._hotContainer = this; 

    // 
    // put any custom stuff here... 
    // 

    // 
    // since inheritance doesn't quite work, the following are all pass-through 
    // functions to cover the fact that composition is being used 
    // 

    this.loadData = function(data) { 
     hot.loadData.apply(this, arguments); 
    } 
    this.render = function() { 
     hot.render.apply(this, arguments); 
    } 
    this.getDataAtRow = function() { 
     return hot.getDataAtRow.apply(this, arguments); 
    } 
    this.countCols = function() { 
     return hot.countCols.apply(this, arguments); 
    } 
    this.sort = function() { 
     hot.sort.apply(this, arguments); 
    } 
}; 
संबंधित मुद्दे