Cache won't work in Appcelerator - caching

Titanium SDK version: 1.6.
iPhone SDK version: 4.2
I am trying out the cache snippet found on the Appcelerator forum but I get an error: [ERROR] Script Error = Can't find variable: utils at cache.js (line 9).
I put this one (http://pastie.org/1541768) in a file called cache.js and implemented the code from this one (http://pastie.org/pastes/1541787) in the calling script, but I get the error.
What is wrong? I copied the code exactly.

Your problems is whilst the first pastie defines utils.httpcache. The variable utils is not defined outside of this function closure (because it is not defined anywhere in global namespace). As below shows.
(function() {
utils.httpcache = {
};
})();
To make it all work in this instance add the following code to the top of your cache.js file.
var utils = {};
This declares the utils variable in global namespace. Then when the function closure is executed below it will add utils.httpcache to the utils object.
The problem is actually not specific to Appcelerator and is just a simple JavaScript bug. Checkout Douglas Crockfords book, JavaScript the Good Parts. Reading it will literally make you a more awesome JavaScript developer.

You can't use utils.httpcache.getFromCache(url) until you add this to your code:
var utils = {};
That's because how the author created his function, it's called JavaScript module pattern and it's generally used to structure the code.

I seem to lose this value "value.httpCacheExpire = expireTime;" when the code does the "Titanium.App.Properties.setString(key,JSON.stringify(value));" so when I get it back using the getString method, there's no longer the "value.httpCacheExpire.
Anyone else have this issue? Am I missing something to get this working?

Related

How to get isHealthDataAvailable from angular2 nativescript using api

Since that is no nativescript support with ios HealthKit..I am trying to work with the api.
To start I am trying to get a simple bool for isHealthDataAvailable():
How?
declare var NSBundle: any;
constructor() {
if (Platform.isIOS){
let healthStore = NSBundle.mainBundle.HKHealthStore();
let is_avail = healthStore.isHealthDataAvailable();
}
}
(in promise): TypeError: NSBundle.mainBundle.HKHealthStore is not a function. (In 'NSBundle.mainBundle.HKHealthStore()', 'NSBundle.mainBundle.HKHealthStore' is undefined)
This is a quote from NativeScript:
https://www.nativescript.org/blog/how-to-enable-healthkit-in-your-nativescript-application
var healthStore = HKHealthStore.new();
And this is how to use HealthKit API in NativeScript. Yes, it is THAT simple.
I beg differ...
That's not where HKHealthStore lives, please see my answer to your other question where I suggest you install the platform declarations (npm i tns-platform-declarations --save-dev). It will help you tremendously: nativescript, angular2 and heaththkit - HKHealthStore not found;
To answer this question: it should be HKHealthStore.isHealthDataAvailable() as can be seen in the screenshot below (which is a snippet of those aforementioned TypeScript declarations for iOS:
You will need to use the proper syntax for data conversion (between the Objective-C Apple API and JavaScript) as described here
e.g.
var healthStore = HKHealthStore.new();
Great detailed step by step explanation can be found in this blog post

Typescript Type definition for d3 sankey

I have some javascript code which uses d3 sankey plugin for creating a chart. In my new project, I need to reuse the same code, but the new project is in typescript. I am looking for a DefinitelyTyped file for the plugin. I browsed through https://github.com/DefinitelyTyped/DefinitelyTyped, but couldn't find it.
Is there any other location where I can get this file from?
Sankey plugin link: https://github.com/d3/d3-sankey
Also, without a d.ts file for this plugin, is there a way to access it through typescript?
The code in d3 plugin looks something like this:
d3.sankey = function () {
// Rest of the code goes here
}
The way I use it in javascript is as below:
d3.sankey().nodeWidth(30).size([100,100]);
Would appreciate any help or guidance.
Thanks!
As a heads-up, I have just submitted a Pull Request #16051 to DefinitelyTyped which contains TS definitions for d3-sankey.
Once they are merged, they will be published as per standard process to npm/#types. I.e. npm install --save-dev #types/d3-sankey will do.
IMPORTANT: When I wrote them up, I noticed that the current API documentation in the d3-sankey repo appears to be in some need of rectification (e.g. missing methods, mentioning of accessor functions, which are not used in the code base)
When I have a second, I will file an issue there/submit a PR.
UPDATE (2017-05-01):
The "official" TypeScript definitions for d3-sankey are now available (see npm #types/d3-sankey). Simply use them with npm as indicated above.
The PR to update the actual API documentation of d3-sankey to reflect the source code is still awaiting a merge here.
You need to expand the definition of the d3 type to include the sankey() method and the methods it accepts.
At the absolute minimum, you need to extend the d3 module with a declaration file to make clear that d3 has been extended with the d3-sankey module. You do so by creating a definition file that you place within the #types directly with the following contents:
declare module 'd3' {
export function sankey(...args[]) : any;
}
This tells TS that there is a d3 module, and that it exports the function listed. If the d3 module already exists, it extends that module.
So you can then import the d3 service and use it:
import dd3 = require('d3');
dd3.sankey();
If you want to expand on the type file, you instead write the definition file as so:
declare module 'd3' {
interface ISankey {
nodeWidth() : number;
nodeWidth(width : number|{(arg: number) : number}) : void;
// Add Other d3.sankey Methods Here
}
export function sankey() : ISankey;
}

Laravel Webpack - Unwanted minification of top level variable

I have a variable in my main javascript file e.g. var example = {};.
After webpack has finished its job, I find that example is now referenced as t. This presents me a problem as I am using the variable across the web project. I bind functions onto objects for example:
var example = {};
example.initialise = function () {};
Finally at the bottom of a page I may invoke this section of script e.g:
<script>example.initialise()</script>
This way of writing javascript functions is not unusual...
This is obviously a huge pain in the ass as I have no control over the minification. Moreover, it appears that webpack doesn't figure out that example.initialise = function () {}; relates to its newly minified var example (becoming)--> var t. I.e. it doesn't become t.initialise = function {}; either.
What am I supposed to do here?
I've tried using rollup as well. The same kind of variable minification happens.
The thing is, this kind of minification/obfuscation is great, particularly on the inner workings of functions where there's little cause for concern over the parameter names. But not on the top level. I do not understand why this is happening, or how to prevent it.
Any ideas?
I assume that there are ways to set the configuration of webpack. E.g. inside webpack.config.js, but my perusing of the webpack docs gives me no easy understanding of what options I can use to resolve this, like preventing property minification in some way.
In laravel-elixir-webpack-official code you can see minify() is being applied here, minify() uses UglifyJS2 and mangling is on by default.
Mangling is an optimisation that reduces names of local variables and functions usually to single-letters (this explains your example object being renamed to t). See the doc here.
I don't see any way you can customize minify() behaviour in laravel-elixir-webpack, so for now you might have to monkey patch WebpackTask.prototype.gulpTask method before using the module (not an ideal solution). See the lines I am commenting out.
const WebpackTask = require('laravel-elixir-webpack-official/dist/WebpackTask').default;
WebpackTask.prototype.gulpTask = function () {
return (
gulp
.src(this.src.path)
.pipe(this.webpack())
.on('error', this.onError())
// .pipe(jsFiles)
// .pipe(this.minify())
// .on('error', this.onError())
// .pipe(jsFiles.restore)
.pipe(this.saveAs(gulp))
.pipe(this.onSuccess())
);
};
Turns out I have been silly. I've discovered that you can prevent top level properties from being minified by binding it to window... which in hindsight is something I've always known and was stupid not to have realised sooner. D'oh!
So all that needed to be done was to change all top-level properties like var example = {}; to something like window.app.example = {}; in which app is helping to namespace and prevent and override anything set by the language itself.

cfajaxproxy is sending invalid parameters?

For some reason that I don't understand, on my development machine can't call to function of a cfc component from a cfajaxproxy.
In my cfm document:
<cfajaxproxy cfc="#Application.CfcPath#.empleado"
jsclassname="ccEmpleado">
This works, and also I can instantiate an object to get all the functions of that cfc component:
var cfcEmpleado = new ccEmpleado();
But, when I try to call a function of that object:
var nb_Empleado = cfcEmpleado.RSEmpeladoNombreBIND(1,1);
Debug complains:
Error: The ID_EMPRESA parameter to the RSEmpeladoNombreBIND function is required but was not passed in.
I got this from Network tab on Chrome and figured out that something is generating an invalid parameter:
http://127.0.0.1/vpa/componentes/empleado.cfc?method=RSEmpeladoNombreBIND&_cf_ajaxproxytoken=[object%20Object]&returnFormat=json&_cf_nodebug=true&_cf_nocache=true&_cf_clientid=41C92098C98042112AE2B3AAF523F289&_cf_rc=0
As you can see, there's a parameter [object%20Object], that is messing around my request, and that's why it fails. I don't why is happening this. Other people has tested this, and it works, but in mine doesn't.
I have Coldfusion 9, Apache, Windows 8. Is is some configuration issue on Coldfusion, or a bug?
I can't tell if this is your error or not, but it might be. This was a problem that we had for awhile. You should consider using explicit names to avoid any confusion. Add the "js" in there.
<cfajaxproxy cfc="cfcEmpleado" jsclassname="proxyEmpleado">
var jsEmpleado = new proxyEmpleado();
I will try to find a link to an article about this very thing.

externs for jQuery Star Rating Plugin and Google Closure Compiler

I created an externs file to be able to compile the jQuery Star Rating Plugin fyneworks.com/jquery/star-rating/#tab-Testing with Google Closure Compiler's ADVANCED_OPTIMIZATIONS.
But, even though I reference the standard jQuery extern, the '$' is getting renamed which breaks the plugin.
Perhaps related: if I use the unmodified plugin, 'rating' also gets renamed. I can fix that part with:
$.fn['rating'] = function(opts) {
from google closure compile jQuery Plugin ... but that doesn't fix '$' (and it would be nice to use the unmodified plugin if possible).
from my attempt at an extern (which is probably wrong and/or incomplete):
// ??? for '$'
// this one does NOT prevent 'rating' from being renamed
function rating(arg1) {}
// the following seem to work: they prevent the functions from being renamed
rating.focus = function() {}
rating.blur = function() {}
rating.fill = function() {}
... etc.
command line (and rating.sh in the download):
java -jar ../compiler-latest/compiler.jar --formatting pretty_print --compilation_level ADVANCED_OPTIMIZATIONS --externs externs/jquery-1.7.js --externs externs/jquery.rating-extern.js --js original/jquery.rating.js --js_output_file jquery.rating.gcc.js
error messages:
Firefox:
$(".star1").rating is not a function
callback: function (value) {
jquery.ratingSampleCode.js (line 9)
Chrome:
Uncaught TypeError: Object [object Object] has no method 'rating'
jquery.ratingSampleCode.js:8
from my sample code:
$('.star1').rating({
callback: function (value) {
To test: http://prefabsoftware.com/test/rating-july15/
To download: prefabsoftware.com/test/rating-july15.zip
Some useful links: (which I'm not allowed to specify as markdown since I couldn't login with my old reputation points...)
Advanced Compilation and Externs: developers.google.com/closure/compiler/docs/api-tutorial3#externs
sample externs: contrib: code.google.com/p/closure-compiler/source/browse/#svn%2Ftrunk%2Fcontrib%2Fexterns) including jQuery itself, but not the rating plugin
more externs: code.google.com/p/closure-compiler/source/browse/#svn%2Ftrunk%2Fexterns
Is there a simple fix for the extern? Or a better solution?
Thanks!
Ok, this works for the externs file:
$.prototype.rating = function(arg1) {}
jQuery.prototype.rating = function(arg1) {}
$.prototype.rating.focus = function() {}
... etc.
From your description, you appear to be using an extern file improperly. An extern file for your plugin would allow other users to compile code referencing your plugin. It shouldn't be used to compile your actual plugin code at all. To compile your code, you would only need the jQuery extern file.
jQuery code styles have known issues with Closure-compiler. In particular, you would need to avoid the following:
Any use of the $ alias. Use the full jQuery namespace. The compiler doesn't handle aliased namespaces well.
The jQuery.fn alias. Instead use jQuery.prototype.
Use of the jQuery.extend method to add function prototypes or public methods. Instead, add them directly to the prototype. (example: jQuery.fn.extend({a: 'foo'}); would become jQuery.prototype.a = 'foo';);
With ADVANCED_OPTIMIZATIONS, keep in mind that you will still have to export or quote any public methods and prototypes. This may mean that SIMPLE_OPTIMIZATIONS turn out to be a better fit for your project.
For more information, see http://blogs.missouristate.edu/web/2011/02/14/jquery-plugins-and-closure-compiler/
Check out the latest externs: https://github.com/iplabs/closure-compiler/tree/master/contrib/externs

Resources