Commit aa51f1fd authored by laurent l's avatar laurent l

more ios port

parent ef63b1d0
import {Couchbase} from 'nativescript-couchbase'; import { Couchbase } from 'nativescript-couchbase';
import {Persistable, Serializable, DataSource} from './persistable'; import { Persistable, Serializable, DataSource } from './persistable';
export class Database implements DataSource { export class Database implements DataSource {
protected static _current: Database; protected static _current: Database;
...@@ -38,13 +38,13 @@ export class Database implements DataSource { ...@@ -38,13 +38,13 @@ export class Database implements DataSource {
return this; return this;
} }
public save(model: Persistable): Database { public save(model: Persistable): Database {
if (!model.validate()) if (!model.validate())
return this; return this;
model.beforeSave(this); model.beforeSave(this);
(new DatabaseTransaction(this, model)).save(); (new DatabaseTransaction(this, model)).save();
model.afterSave(this); model.afterSave(this);
...@@ -60,17 +60,17 @@ export class Database implements DataSource { ...@@ -60,17 +60,17 @@ export class Database implements DataSource {
} }
public findAll<T extends Persistable>(modelClass:{new():T;}): Array<T> { public findAll<T extends Persistable>(modelClass: { new(): T; }): Array<T> {
return this return this
._views ._views
.findAll(modelClass, .findAll(modelClass,
(document) => { (document) => {
return this._materialize(modelClass, document); return this._materialize(modelClass, document);
}); });
} }
public find<T extends Persistable>(modelClass:{new():T;}, id:string): T { public find<T extends Persistable>(modelClass: { new(): T; }, id: string): T {
if (!id) if (!id)
return null; return null;
...@@ -82,12 +82,12 @@ export class Database implements DataSource { ...@@ -82,12 +82,12 @@ export class Database implements DataSource {
} }
public createView(name:string, revision:string, callback:Function) { public createView(name: string, revision: string, callback: Function) {
return this._views.createView(name, revision, callback); return this._views.createView(name, revision, callback);
} }
public executeQuery(name:string) { public executeQuery(name: string) {
return this._db.executeQuery(name); return this._db.executeQuery(name);
} }
...@@ -102,7 +102,7 @@ export class Database implements DataSource { ...@@ -102,7 +102,7 @@ export class Database implements DataSource {
} }
protected _materialize<T extends Persistable>(modelClass: {new(id:string):T;}, document:any): T { protected _materialize<T extends Persistable>(modelClass: { new(id: string): T; }, document: any): T {
let model: T = new modelClass(document._id); let model: T = new modelClass(document._id);
model.materializeFrom(new DatabaseDocument(this, document)); model.materializeFrom(new DatabaseDocument(this, document));
return model; return model;
...@@ -111,7 +111,6 @@ export class Database implements DataSource { ...@@ -111,7 +111,6 @@ export class Database implements DataSource {
class DatabaseViews { class DatabaseViews {
protected _db: Couchbase; protected _db: Couchbase;
protected _declared_views: Set<string>; protected _declared_views: Set<string>;
...@@ -122,24 +121,24 @@ class DatabaseViews { ...@@ -122,24 +121,24 @@ class DatabaseViews {
} }
public createView(name:string, revision:string, callback:Function): DatabaseViews { public createView(name: string, revision: string, callback: Function): DatabaseViews {
this._declared_views.add(name); this._declared_views.add(name);
this._db.createView(name, revision, callback); this._db.createView(name, revision, callback);
return this; return this;
} }
public executeQueryForModel(modelName:string) { public executeQueryForModel(modelName: string) {
let viewname = "all-" + modelName.toLowerCase() + "s"; let viewname = "all-" + modelName.toLowerCase() + "s";
this._ensureView(viewname, modelName); this._ensureView(viewname, modelName);
return this._db.executeQuery(viewname); return this._db.executeQuery(viewname);
} }
public findAll<T extends Persistable>(modelClass:{new():T;}, callback:Function): Array<T> { public findAll<T extends Persistable>(modelClass: { new(): T; }, callback: Function): Array<T> {
let models: Array<T> = new Array<T>(); let models: Array<T> = new Array<T>();
for(let document of this.executeQueryForModel(modelClass.name)) { for (let document of this.executeQueryForModel(modelClass.name)) {
models.push(callback(document)); models.push(callback(document));
}; };
return models; return models;
...@@ -151,12 +150,12 @@ class DatabaseViews { ...@@ -151,12 +150,12 @@ class DatabaseViews {
return this; return this;
this.createView(viewname, this.createView(viewname,
"1", "1",
(document, emitter) => { (document, emitter) => {
if (document.model == modelName) { if (document.model == modelName) {
emitter.emit(document._id, document); emitter.emit(document._id, document);
} }
}); });
} }
} }
...@@ -168,7 +167,7 @@ class DatabaseTransaction { ...@@ -168,7 +167,7 @@ class DatabaseTransaction {
protected _model: Persistable; protected _model: Persistable;
public constructor(db:Database, model:Persistable) { public constructor(db: Database, model: Persistable) {
this._db = db; this._db = db;
this._model = model; this._model = model;
} }
...@@ -195,7 +194,7 @@ class DatabaseTransaction { ...@@ -195,7 +194,7 @@ class DatabaseTransaction {
protected _update(document): DatabaseTransaction { protected _update(document): DatabaseTransaction {
this._db.updateDocument(this._model.getId(), this._db.updateDocument(this._model.getId(),
document.serialize()); document.serialize());
return this; return this;
} }
} }
...@@ -207,19 +206,19 @@ class DatabaseDocument implements Serializable { ...@@ -207,19 +206,19 @@ class DatabaseDocument implements Serializable {
protected _properties = {}; protected _properties = {};
protected _db: Database; protected _db: Database;
public constructor(db: Database, properties?:Object) { public constructor(db: Database, properties?: Object) {
this._db = db; this._db = db;
this._properties = properties ? properties : {}; this._properties = properties ? properties : {};
} }
public set(name:string, value:number|string|boolean): Serializable { public set(name: string, value: number | string | boolean): Serializable {
this._properties[name] = (null == value ? '' : value); this._properties[name] = (null == value ? '' : value);
return this; return this;
} }
public get<T extends Persistable>(name:string, modelClass?:{new():T;}): number|string|boolean|T { public get<T extends Persistable>(name: string, modelClass?: { new(): T; }): number | string | boolean | T {
return modelClass return modelClass
? this._db.find(modelClass, this._properties[name]) ? this._db.find(modelClass, this._properties[name])
: this._properties[name]; : this._properties[name];
...@@ -227,6 +226,6 @@ class DatabaseDocument implements Serializable { ...@@ -227,6 +226,6 @@ class DatabaseDocument implements Serializable {
public serialize(): Object { public serialize(): Object {
return this._properties; return JSON.parse(JSON.stringify(this._properties));
} }
} }
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
android.windowContentOverlay="@drawable/actionbar_shadow"> android.windowContentOverlay="@drawable/actionbar_shadow">
<NavigationButton icon="res://icon" tap="{{ showAboutAction }}"/> <NavigationButton icon="res://icon" tap="{{ showAboutAction }}"/>
<ActionItem text="{{ L('menu') }}" tap="{{ toggleDrawerAction }}"> <ActionItem text="{{ L('menu') }}" tap="{{ toggleDrawerAction }}" ios.position="right">
<ActionItem.actionView> <ActionItem.actionView>
<Label text='&#xf0c9;'/> <Label text='&#xf0c9;'/>
</ActionItem.actionView> </ActionItem.actionView>
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<ActionBar title="{{ L('accounts') }}" icon=""> <ActionBar title="{{ L('accounts') }}" icon="">
<NavigationButton icon="res://icon" tap="{{ showAboutAction }}"/> <NavigationButton icon="res://icon" tap="{{ showAboutAction }}"/>
<ActionItem text="{{ L('menu') }}" tap="{{ toggleDrawerAction }}"> <ActionItem text="{{ L('menu') }}" tap="{{ toggleDrawerAction }}" ios.position="right">
<ActionItem.actionView> <ActionItem.actionView>
<Label text='&#xf0c9;'/> <Label text='&#xf0c9;'/>
</ActionItem.actionView> </ActionItem.actionView>
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<ActionBar title="{{ page_title }}" icon=""> <ActionBar title="{{ page_title }}" icon="">
<NavigationButton android.systemIcon="ic_menu_back" tap="{{ goBack }}"/> <NavigationButton android.systemIcon="ic_menu_back" tap="{{ goBack }}"/>
<ActionItem text="{{ L('menu') }}" tap="{{ toggleDrawerAction }}"> <ActionItem text="{{ L('menu') }}" tap="{{ toggleDrawerAction }}" ios.position="right">
<ActionItem.actionView> <ActionItem.actionView>
<Label text='&#xf0c9;'/> <Label text='&#xf0c9;'/>
</ActionItem.actionView> </ActionItem.actionView>
......
...@@ -3,13 +3,13 @@ ...@@ -3,13 +3,13 @@
<ActionBar title="{{ page_title }}" icon=""> <ActionBar title="{{ page_title }}" icon="">
<NavigationButton android.systemIcon="ic_menu_back" tap="{{ goHomeAction }}"/> <NavigationButton android.systemIcon="ic_menu_back" tap="{{ goHomeAction }}"/>
<ActionItem text="{{ L('refresh') }}" tap="{{ refreshLoansAndHoldsAction }}"> <ActionItem text="{{ L('refresh') }}" tap="{{ refreshLoansAndHoldsAction }}" ios.position="right">
<ActionItem.actionView> <ActionItem.actionView>
<Label text='&#xf021;'/> <Label text='&#xf021;'/>
</ActionItem.actionView> </ActionItem.actionView>
</ActionItem> </ActionItem>
<ActionItem text="{{ L('menu') }}" tap="{{ toggleDrawerAction }}"> <ActionItem text="{{ L('menu') }}" tap="{{ toggleDrawerAction }}" ios.position="right">
<ActionItem.actionView> <ActionItem.actionView>
<Label text='&#xf0c9;'/> <Label text='&#xf0c9;'/>
</ActionItem.actionView> </ActionItem.actionView>
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<Page.actionBar> <Page.actionBar>
<ActionBar title="{{ L('novelties') }}" icon=""> <ActionBar title="{{ L('novelties') }}" icon="">
<NavigationButton android.systemIcon="ic_menu_back" tap="{{ goBack }}"/> <NavigationButton android.systemIcon="ic_menu_back" tap="{{ goBack }}"/>
<ActionItem text="{{ L('menu') }}" tap="{{ toggleDrawerAction }}"> <ActionItem text="{{ L('menu') }}" tap="{{ toggleDrawerAction }}" ios.position="right">
<ActionItem.actionView> <ActionItem.actionView>
<Label text='&#xf0c9;'/> <Label text='&#xf0c9;'/>
</ActionItem.actionView> </ActionItem.actionView>
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<ActionBar title="{{ page_title }}" icon=""> <ActionBar title="{{ page_title }}" icon="">
<NavigationButton android.systemIcon="ic_menu_back" tap="{{ goBack }}"/> <NavigationButton android.systemIcon="ic_menu_back" tap="{{ goBack }}"/>
<ActionItem text="{{ L('menu') }}" tap="{{ toggleDrawerAction }}"> <ActionItem text="{{ L('menu') }}" tap="{{ toggleDrawerAction }}" ios.position="right">
<ActionItem.actionView> <ActionItem.actionView>
<Label text='&#xf0c9;'/> <Label text='&#xf0c9;'/>
</ActionItem.actionView> </ActionItem.actionView>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment