简体   繁体   English

在pebble js文件中包含一个外部javascript库?

[英]Including an external javascript library in pebble js file?

Is there any way I can include an external JS library in my pebble code? 有什么办法可以在我的卵石代码中包含一个外部JS库吗? Conventionally on a webpage I would do this in my head tags: 通常在网页上我会在我的头标签中执行此操作:

<script type='text/javascript' src='https://cdn.firebase.com/js/client/1.0.11/firebase.js'></script>

But in pebble, I am unable to do that since I am only using JS. 但是在鹅卵石中,我无法做到这一点,因为我只使用JS。 So how can I include an external library for a JavaScript file. 那么我如何为JavaScript文件包含一个外部库。

At present, you cannot include external JS files. 目前,您不能包含外部JS文件。

If you're using CloudPebble, then the only way to do this is to copy and paste the content of the JS library files into your JS file. 如果您正在使用CloudPebble,那么唯一的方法是将JS库文件的内容复制并粘贴到您的JS文件中。

If you're doing native development, you can modify the wscript file to combine multiple JS files into one at build time. 如果您正在进行本机开发,则可以修改wscript文件以在构建时将多个JS文件合并为一个。

I think there's some confusion over Pebble.js vs PebbleKit JS (v3.8.1). 我认为Pebble.js与PebbleKit JS(v3.8.1)存在一些混淆。 Pebble.js is a fledgling SDK where eventually the programmer will be able to write pure JavaScript. Pebble.js是一个初出茅庐的SDK,最终程序员将能够编写纯JavaScript。 It's still cooking so there's some functionality missing like the ability to draw a line or obtain the screen dimensions. 它仍在烹饪,因此缺少某些功能,如绘制线条或获取屏幕尺寸的功能。 The repo is a mix of C and JS sources where you can add C code to augment missing functionality but otherwise all your code lives in src/js/app.js or src/js/app/ . repo是C和JS源的混合,你可以添加C代码来增加缺失的功能,但是你的所有代码都存在于src/js/app.jssrc/js/app/ Anyway, Pebble.js does support require . 无论如何,Pebble.js确实支持 require

I don't use CloudPebble but I got the impression that it either supports Pebble.js (and hence require ) or is planning to. 我不使用CloudPebble,但我得到的印象是它支持Pebble.js(因此require )或计划。 I think all this SDK boilerplate code would be hidden. 我认为所有这些SDK样板代码都将被隐藏。

PebbleKit JS does not support require out of the box AFAIK. PebbleKit JS不支持require开箱AFAIK的。 I've made a demo that ports require support from Pebble.js to PKJS. 我做了一个演示 ,端口require Pebble.js支持PKJS。 The summary of changes is: 变更摘要如下:

  1. Move your project's src/js/pebble-js-app.js to src/js/app/index.js . 将项目的src/js/pebble-js-app.jssrc/js/app/index.js
  2. Remove any ready event listener from src/js/app/index.js . src/js/app/index.js删除所有ready事件侦听器。 index.js will be loaded when the ready event is emitted. 发出ready事件时将加载index.js
  3. Add src/js/loader.js from Pebble.js. 从Pebble.js添加src/js/loader.js loader.js。
  4. Add a src/js/main.js that calls require('src/js/app') on the ready event. ready事件上添加一个调用require('src/js/app')src/js/main.js
  5. Update your wscript with the following deltas . 使用以下增量更新您的wscript
  6. When adding new modules, place them under src/js/app/ and require('./name') will work. 添加新模块时,将它们放在src/js/app/require('./name')将起作用。

I've tried to cover this all in the demo's readme . 我试着在演示的自述文件中介绍这一切。

BTW, here's the official breakdown of all the different SDKs but it's a little confusing. 顺便说一句,这是所有不同SDK官方细分,但有点令人困惑。

I am not sure if there have been changes since the above answer, but it looks like there is in fact a way to include additional resources while keeping things tidy. 我不确定自上述答案以来是否有变化,但看起来实际上有一种方法可以在保持整洁的同时包含额外的资源。 On the pebbleJS page , there is the following section with an some information on the subject. pebbleJS页面上 ,以下部分提供了有关该主题的一些信息。


GLOBAL NAMESPACE - require(path) 全球NAMESPACE - 要求(路径)

Loads another JavaScript file allowing you to write a multi-file project. 加载另一个JavaScript文件,允许您编写多文件项目。 Package loading loosely follows the CommonJS format. 程序包加载松散地遵循CommonJS格式。 path is the path to the dependency. path是依赖项的路径。


You can then use the following code to "require" a JS library in your pebble project. 然后,您可以使用以下代码在pebble项目中“需要”JS库。 This should be usable on both Cloud Pebble as well as native development. 这应该可以在Cloud Pebble和本地开发中使用。

// src/js/dependency.js
var dep = require('dependency');

You can then use this as shown below: 然后您可以使用它,如下所示:

dep.myFunction(); // run a function from the dependency
dep.myVar = 2; // access or change variables

Update: After some digging into this for my own, I have successfully gotten CloudPebble to work with this functionality. 更新:在为我自己进行了一些挖掘之后,我成功地让CloudPebble使用了这个功能。 It is a bit convoluted, but follows the ECMAScript 6 standards. 它有点复杂,但遵循ECMAScript 6标准。 Below I am posting a simple example of getting things set up. 下面我发布一个简单的例子来设置东西。 Additionally, I would suggest looking at this code from PebbleJS for a better reference of a complex setup. 另外,我建议从PebbleJS查看此代码,以便更好地参考复杂的设置。

myApp.js myApp.js

var resource = require('myExtraFile');        // require additional library

console.log(resource.value);                  // logs 42

resource.functionA();                         // logs "This is working now"

myExtraFile.js myExtraFile.js

var myExtraFile = {                           // create a JSON object to export

  "value" : 42,                               // variable

  functionA : function() {                    // function
    console.log("This is working now!");
  }
};

this.exports = myExtraFile;                   // export this function for
                                              // later use

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM