使用jquery访问另一个js文件

时间:2015-05-14 20:49:03

标签: javascript jquery

我试图访问其他javascript文件中的数据,并且它是这样写的:

.cs

在HTML中,它嵌入在我的脚本文件之前,意思是

(function($) {

  var Poller = function () {
    this.defaults = {
      type: 'veggies',
      limit: 10
    };

    this.items = {
      veggies: [
        'Adzuki Beans',
        'Asparagus',
        'Black-eyed Peas',
        'Brussels Sprouts',
        'Carrots',
        'Collard Greens',
        'Parsnips',
        'Rhubarb',
        'Yams',
        'Watercress'
      ],
      fruits: [
        'Apricots',
        'Blackcurrants',
        'Cherimoya',
        'Dates',
        'Elderberry',
        'Guava',
        'Kumquat',
        'Miracle Fruit',
        'Purple Mangosteen',
        'Satsuma'
      ]
    };
  };

  Poller.prototype._getRandomNumber = function (min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  };

  Poller.prototype._getData = function (type) {
    var item, i, len;
    var list = this.items[type] || [];
    var results = [];

    for (i = 0, len = list.length; i < len; i++) {
      item = list[i];

      results.push({
        name: item,
        count: this._getRandomNumber(0, 200000)
      });
    }
    return results;
  };

  Poller.prototype._processData = function (data, limit) {
    return data.slice(0, limit);
  };

  Poller.prototype.poll = function (options, cb) {
    var self = this;
    var config = $.extend({}, this.defaults, options);
    var dfd = $.Deferred();

    setTimeout(function () {
      var payload = self._processData(self._getData(config.type), config.limit);

      cb && cb(payload);      
      dfd.resolve(payload);
    }, this._getRandomNumber(400, 2000));

    return dfd;
  };

  if (window.spredfast == null) {
    window.spredfast = {
      Poller: Poller
    };
  }
}(jQuery));

如何从我的脚本文件中访问数据并按降序混合蔬菜和水果显示?

谢谢,

2 个答案:

答案 0 :(得分:0)

应该工作......

if (!(spredfast in window)) {
  window.spredfast = {
      Poller: function(){
        return new PollerInstance();
      }
  };
} else {
  window.spredfast.Poller = function(){
    return new PollerInstance();
  };
}

// call
var Poller = spredfast.Poller();

答案 1 :(得分:0)

运行此命令以访问数据items.veggies。必须创建一个新的Poller实例。

(function($) {

  var Poller = function () {
    
    this.defaults = {
      type: 'veggies',
      limit: 10
    };

    this.items = {
      veggies: [
        'Adzuki Beans',
        'Asparagus',
        'Black-eyed Peas',
        'Brussels Sprouts',
        'Carrots',
        'Collard Greens',
        'Parsnips',
        'Rhubarb',
        'Yams',
        'Watercress'
      ],
      fruits: [
        'Apricots',
        'Blackcurrants',
        'Cherimoya',
        'Dates',
        'Elderberry',
        'Guava',
        'Kumquat',
        'Miracle Fruit',
        'Purple Mangosteen',
        'Satsuma'
      ]
    };
  };

  Poller.prototype._getRandomNumber = function (min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  };

  Poller.prototype._getData = function (type) {
    var item, i, len;
    var list = this.items[type] || [];
    var results = [];

    for (i = 0, len = list.length; i < len; i++) {
      item = list[i];

      results.push({
        name: item,
        count: this._getRandomNumber(0, 200000)
      });
    }
    return results;
  };

  Poller.prototype._processData = function (data, limit) {
    return data.slice(0, limit);
  };

  Poller.prototype.poll = function (options, cb) {
    var self = this;
    var config = $.extend({}, this.defaults, options);
    var dfd = $.Deferred();

    setTimeout(function () {
      var payload = self._processData(self._getData(config.type), config.limit);

      cb && cb(payload);      
      dfd.resolve(payload);
    }, this._getRandomNumber(400, 2000));

    return dfd;
  };

  if (window.spredfast == null) {
    
    window.spredfast = {
      Poller: new Poller()
    };
  }
}(jQuery));


var dfd = window.spredfast.Poller.poll().then(function(data) {
  
  data.sort(function(a, b){
    return a.count < b.count;
  });
  
  var message = '';
  for(var i = 0; i < data.length; i++){
    message += data[i].name + ': ' + data[i].count + '\n';
  }
  
  alert(message);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>