PDA

View Full Version : Adding custom data proxy to toolbox



mehran
20 Feb 2012, 6:41 AM
Hi

I wonder if it is possible to add a custom data proxy to toolbox, as the same way we can add custom components to it?

aconran
20 Feb 2012, 7:17 AM
Currently you cannot save proxies to the toolbox.

We will look at adding this type of behavior in the future.

dirk.hamberger
13 Apr 2012, 1:46 AM
Is there a way to configure a custom proxy on a store with some pattern? Maybe with an override Class to a store? Currently it's not possible to choose other than the default types in the store/model property toolbox. At least it would be nice to give a configuration object into the 'proxy' property field, containing a custom xtype and maybe some configuration needed for a custom proxy class (as I have to - currently writing a Jayrock Proxy ... some 'other' kind of 'direct' Proxy).

aconran
13 Apr 2012, 9:50 AM
Yes, this is possible by linking in an additional proxy via the Resource Manager and then implementing an override.

fjrodriguez
13 Apr 2012, 11:09 AM
Yes, this is possible by linking in an additional proxy via the Resource Manager and then implementing an override.

I couldn't do that.

I'm trying to implement a sqlite proxy.

a) Resource Manager add .js link after all others. Is it the right place?
b) Doing an override doesn't seems to take new configured proxy.

I agree a custom proxy in the toolbox would be great.

aconran
13 Apr 2012, 11:12 AM
a) Resource Manager add .js link after all others. Is it the right place?

Yes.


b) Doing an override doesn't seems to take new configured proxy.
Did you override the constructor of the Store? Is it getting run?

fjrodriguez
14 Apr 2012, 7:58 AM
Did you override the constructor of the Store? Is it getting run?

obviously I'm not doing well...
I added .js resources SqliteConnection and SqliteProxy.
I added

var dbconnval = {
dbName: "myModel",
dbDescription: "testdb"
};
Ext.DbConnection = Ext.create('Ext.Sqlite.Connection',dbconnval);
}

on launch : function() in Application Entry Point.

how can I add a custom proxy in a model in the constructor?

I try to put a console.log in the constructor: function(cfg) of the override class but it doesn't show it and the several test I did trying to add the proxy ended badly but without errors.

Thanks.

fjrodriguez
15 Apr 2012, 11:16 AM
One of my last (and desperate) attempt


Ext.define('MyApp.model.override.MyModel', {
requires: 'MyApp.model.MyModel'
}, function() {
Ext.override(MyApp.model.MyModel, {
writer : { type: 'array' },
constructor : function(config)
{
config = config || {};

var proxySql = {
type: 'sqlitestorage',
DbConfig :
{
tablename : 'tabla1',
dbConn : Ext.DbConnection
},
reader : { type: 'array' }
};


config.proxy = Ext.create('proxy.sqlitestorage', proxySql);

this.callParent(config);
}

});
});

dirk.hamberger
15 Apr 2012, 9:43 PM
I think 'callParent' here won't call the contructor of your 'MyModel' class, but the Ext.data.Model contructor, which is 'too few'. I succeeded with that pattern:


Ext.define('UM.store.override.Users', {
requires: 'UM.store.Users'
}, function() {

var overriddenConstructor = UM.store.Users.prototype.constructor;

Ext.override(UM.store.Users, {
constructor: function(cfg){
cfg = cfg || {};
Ext.apply(cfg, {
proxy: {
type: 'jayrockrpc',
rpcHandler: 'UserManagementHandlerMockup',
api: {
create: 'createUser',
read: 'readUser',
update: 'updateUser',
destroy: 'destroyUser'
}
}
});
overriddenConstructor.apply(this, [cfg]);
}
});
});

However, surprisingly, all stores configured in the Application (stores: ...) having a storeId don't run through this constructor at all, I have to recreate them manually after launch, overwriting the previously registered stores in the store manager.



launch: function() {
var me = this;
// (re-)create User Model and Store
// (because of proxy override!)
Ext.create('UM.store.User');
...


Just after this, they pass my constructor and have my custom proxy configured.

fjrodriguez
17 Apr 2012, 2:24 AM
Is there any other possibility without re-create in launch function?

Why could not be possible to create constructor in UM.store.Users adding the proxy?

worthlutz
27 Jun 2012, 10:40 AM
After searching everywhere and ending up at this thread, here is what I did.
I created an override for my store using the example above:

Ext.define(
'CollectIt.store.override.SavedLocations',
{ requires: 'CollectIt.store.SavedLocations' },
function() {
var overriddenConstructor = CollectIt.store.SavedLocations.prototype.constructor;
Ext.override(
CollectIt.store.SavedLocations,
{
constructor: function (config) {
config = config || {};
Ext.apply(
config,
{
proxy: {
type: 'cordovafile',
reader: {
type: 'json'
},
writer: {
type: 'json'
}
}
}
);
overriddenConstructor.apply(this, [config]);
}

}
);
}
);

And then I added the proxy code to a new directory "extras" in the "app" directory of my project. This directory is not overwritten by Architect when saving a project and gets copied by build.

example: CollectIt.app.extras.CordovaFileProxy.js

I added this file to the "requires" of the application.


Then here is the skeleton of my new proxy:


Ext.define( 'CollectIt.extras.CordovaFileProxy',
{ extend: 'Ext.data.proxy.Proxy',
alias: 'proxy.cordovafile',
create: function (operation, callback, scope) {},
read: function (operation, callback, scope) {
var app = CollectIt.app,
thisProxy = this,
reader = this.getReader(),


fail = function (error) {
console.log('**** CordovaFileProxy Error ****' + error.code);
},

getAppDirectory = function (fileSystem) {
fileSystem.root.getDirectory(app.getName(), {create: true}, getFile, fail);
},

getFile = function (dir) {
dir.getFile("SavedLocations.json", {create: true}, readFile, fail);
},

readFile = function (fileEntry) {
var fileReader = new FileReader();


fileReader.onloadend = function (e) {
// e.target.result is the text string from the file
operation.resultSet = reader.read(e.target.result);
operation.setSuccessful();
operation.setCompleted();
if(typeof callback === "function") {
callback.call( scope || thisProxy, operation );
}
};

fileReader.readAsText(fileEntry);
};

if (window.requestFileSystem) {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, getAppDirectory, fail);
}
},
update: function (operation, callback, scope) {},
destroy: function (operation, callback, scope) {}
}
);

It seems to be working so far. I did not have to do anything in the application "launch" function.

I'd appreciate comments and suggestions.
Worth