theannouncer
theannouncer

Reputation: 1212

extjs - Add data to Store from external Javascript

I have a chart with a store using EXTJS4 MVC structure and I get data from an online api via another javascript file and I want to know if it possible to add the data to the store externally so it will load when I call store.load().

The external js is not extjs or part of the mvc, it runs on the page behind my chart. Would I have to transfer all the logic into an extjs controller? Is there a way to call a extjs method from an external js? Thanks.

Upvotes: 0

Views: 1692

Answers (2)

theannouncer
theannouncer

Reputation: 1212

Ended up using the following to access a local js file.

var url = "url.js";
var onload = function () {
    console.log('url loaded');
};
var onerror = function () {
    console.log('error loading analytics functions');
};
Ext.Loader.injectScriptElement(url, onload, onerror, scope);

make sure in your app.js you start the Ext.Loader like so:

Ext.Loader.setConfig({enabled:true});

Upvotes: 0

dougajmcdonald
dougajmcdonald

Reputation: 20037

There are indeed ways to call methods on ExtJS objects from outside of the ExtJS specific code, it's just JS after all.

You will need to expose a method from your ExtJS code which returns a reference to say the chart, something along these lines (in ExtJS code):

function getChart() {

    return Ext.getCmp('myChartID');

}

Then in your external JS, you can do something like this:

var data = ...; // This is where you can get your external data

var chart = getChart();

chart.load(data);

This depends on the ability to expose a method from your ExtJS so you'll need to be able to reference.

It may well be easier to try and call the method to retrieve you data from in the ExtJS MVC app. Providing your external JS file is loaded, it will presumably return a reference to itself to you can call it's methods. So with that in mind, just call the methods you need from inside the ExtJS app as an alternative.

Upvotes: 1

Related Questions