1

我是 pebble js app dev 的新手,想要获取URL的 json 。所以我遇到了RSVP模块,但是我收到以下代码的错误:

/**
 * Welcome to Pebble.js!
 *
 * This is where you write your app.
 */

var UI = require('ui');
var ajax = require('ajax');
var RSVP = require('rsvp');

var Vector2 = require('vector2');
var userName = 'lamiastella';
var URL = 'http://www.last.fm/user/' + userName;
var card = new UI.Card({
  title:'last.fm stat',
  subtitle:'Fetching...'
});
card.show();


var getJSON = function(url) {
  var promise = new RSVP.Promise(function(resolve, reject){
    var client = new XMLHttpRequest();
    client.open("GET", url);
    client.onreadystatechange = handler;
    client.responseType = "json";
    client.setRequestHeader("Accept", "application/json");
    client.send();

    function handler() {
      if (this.readyState === this.DONE) {
        if (this.status === 200) { resolve(this.response); }
        else { reject(this); }
      }
    };
  });

  return promise;
};


ajax({ url: URL }, function(data){
 // var headline = data.match(/(.*?)<\/h1>/)[1];
  var title= data.match(/<h1>(.*?)<\/h1>/);
  card.subtitle(title);
  //card.subtitle(headline);
});

错误是:

[PHONE] pebble-app.js:?: JavaScript Error:
Error: Cannot find module 'rsvp'
at Object.loader.require (loader.js:34:11)
at require (loader.js:41:48)
at Object.loader (app.js:9:12)
at Object.loader.require (loader.js:44:10)
at require (loader.js:41:48)

PS:如果使用 RSVP 不是要走的路,我怎么能说提取“顶级艺术家”或类似信息?

4

2 回答 2

2

I am using RSVP in my Pebble.js app. Here's how I'm requiring it:

var RSVP = require('./rsvp.js');

The only problem is that it doesn't work in the emulator, due to a missing global variable in that environment.

于 2015-07-25T08:17:52.983 回答
1

不幸的是,您不能在 Pebble.js 中包含任意库。

在您的特定情况下,RSVP 有点矫枉过正,您可以在 ajax 请求中嵌套另一个回调。

于 2015-07-14T17:37:28.187 回答