如何在JavaScript对象中调用函数属性?

时间:2018-09-03 08:48:22

标签: javascript jquery object

我有一个用于动态窗口小部件的jQuery插件,称为jarvisWidgets,包含在[smartadmin模板] [1]中。但是我发现很难在此插件中调用函数。

我想从外部插件调用_loadAjaxFile函数,可以吗?

(function($, window, document, undefined) {
  //"use strict"; 

  var pluginName = 'jarvisWidgets';

  /**
   * Check for touch support and set right click events.
   **/
  var clickEvent = (('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch ?
    'clickEvent' : 'click') + '.' + pluginName;

  function Plugin(element, options) {
    /**
     * Variables.
     **/
    this.obj = $(element);
    this.o = $.extend({}, $.fn[pluginName].defaults, options);
    this.objId = this.obj.attr('id');
    this.pwCtrls = '.jarviswidget-ctrls';
    this.widget = this.obj.find(this.o.widgets);
    this.toggleClass = this.o.toggleClass.split('|');
    this.editClass = this.o.editClass.split('|');
    this.fullscreenClass = this.o.fullscreenClass.split('|');
    this.customClass = this.o.customClass.split('|');
    this.storage = {
      enabled: this.o.localStorage
    };
    this.initialized = false;
    this.init();
  }

  Plugin.prototype = {
    /**
     * Function for the indicator image.
     *
     * @param:
     **/
    _runLoaderWidget: function(elm) {
      var self = this;
      if (self.o.indicator === true) {
        elm.parents(self.o.widgets)
          .find('.jarviswidget-loader:first')
          .stop(true, true)
          .fadeIn(100)
          .delay(self.o.indicatorTime)
          .fadeOut(100);
      }
    },

    /**
     * Create a fixed timestamp.
     *
     * @param: t | date | Current date.
     **/
    _getPastTimestamp: function(t) {
      var self = this;
      var da = new Date(t);

      /**
       * Get and set the date and time.
       **/
      var tsMonth = da.getMonth() + 1;
      // index based
      var tsDay = da.getDate();
      var tsYear = da.getFullYear();
      var tsHours = da.getHours();
      var tsMinutes = da.getMinutes();
      var tsSeconds = da.getUTCSeconds();

      /**
       * Checking for one digit values, if so add an zero.
       **/
      if (tsMonth < 10) {
        tsMonth = '0' + tsMonth;
      }
      if (tsDay < 10) {
        tsDay = '0' + tsDay;
      }
      if (tsHours < 10) {
        tsHours = '0' + tsHours;
      }
      if (tsMinutes < 10) {
        tsMinutes = '0' + tsMinutes;
      }
      if (tsSeconds < 10) {
        tsSeconds = '0' + tsSeconds;
      }

      /**
       * The output, how you want it.
       **/
      var format = self.o.timestampFormat.replace(/%d%/g, tsDay)
        .replace(/%m%/g, tsMonth)
        .replace(/%y%/g, tsYear)
        .replace(/%h%/g, tsHours)
        .replace(/%i%/g, tsMinutes)
        .replace(/%s%/g, tsSeconds);

      return format;
    },

    /**
     * AJAX load File, which get and shows the .
     *
     * @param: awidget | object  | The widget.
     * @param: file    | file    | The file thats beeing loaded.
     * @param: loader  | object  | The widget.
     **/
    _loadAjaxFile: function(awidget, file, loader) {
      var self = this;

      awidget.find('.widget-body').load(file, function(response, status, xhr) {
        var $this = $(this);

        /**
         * If action runs into an error display an error msg.
         **/
        if (status == "error") {
          $this.html('<h4 class="alert alert-danger">' + self.o.labelError + '<b> ' +
            xhr.status + " " + xhr.statusText + '</b></h4>');
        }

        /**
         * Run if there are no errors.
         **/
        if (status == "success") {
          /**
           * Show a timestamp.
           **/
          var aPalceholder = awidget.find(self.o.timestampPlaceholder);

          if (aPalceholder.length) {
            aPalceholder.html(self._getPastTimestamp(new Date()));
          }

          /**
           * Run the callback function.
           **/
          if (typeof self.o.afterLoad == 'function') {
            self.o.afterLoad.call(this, awidget);
          }
        }

        self = null;
      });

      /**
       * Run function for the indicator image.
       **/
      this._runLoaderWidget(loader);

    },
    // other functions
  })(jQuery, window, document);

0 个答案:

没有答案