Define js data resource in TypeScript

Is it possible to create a js data resource definition using the TypeScript class?

In general, I would like to have full input support for computed method definitions and instance instances.

What would be surprising is something like this:

class SomeModel { public someBusinessModelValue = 'foo'; public someMoreValues = 'bar'; public get someComputedProperty() { return this.someBusinessModelValue + someMoreValues; } public instanceMethod(param: string) { return this.someMoveValues.search(param); } } 

and then

 DS.defineResource(fromClass('name', '/endpoint', 'idAttr', SomeModel)); 

or go even further and define it as

 class SomeModelStore extends SomeModel { name = 'name'; endpoint = 'endpoint'; idAttribute = 'idAttr'; relations = { //[...] } } 

and use it like

 DS.defineResource(SomeModelStore); 

Please note that these are just some thoughts on what I hope this will look like, I know that it probably does not work exactly like that.

+6
source share
1 answer

JSData 2.x

The answer is yes. Creating resource definitions in JSData 2.x is not very flexible, but you can provide a constructor function (via the useClass option) that will be used when creating the record instance.

Here is an example: http://plnkr.co/edit/vNCoC8?p=info and useClass documentation: http://www.js-data.io/docs/dsdefaults#useclass

JSData 3.x

In JSData 3.x, you can simply extend the various classes:

 import { DataStore, Mapper, Record } from 'js-data'; class CustomMapper extends Mapper { // ... } const store = new DataStore({ mapperClass: CustomMapper }); class BaseCustomRecord extends Record { // ... } store.defineMapper('user', { recordClass: class UserRecord extends BaseCustomRecord { /*...*/ } }); store.defineMapper('post', { recordClass: class PostRecord extends BaseCustomRecord { /*...*/ } }); store.defineMapper('comment', { recordClass: class CommentRecord extends BaseCustomRecord { /*...*/ } }); // etc. etc. 

Here are some plunkers that show that some classes extend with JSData 3.x:

And API documents are a convenient resource when extending classes: http://api.js-data.io/js-data/latest/index.html

+4
source

All Articles