Example #1
0
Ventas.algebra = new set.Algebra(
	set.comparators.id('_id')
);

Ventas.connection = connect(
	[
		feathersServiceBehavior
	,	dataParse
	,	construct
	,	constructStore
	,	constructOnce
	,	canMap
	,	canRef
	,	dataCallbacks
	,	realtime
	]
,	{
		idProp:	'_id'
	,	Map:	Ventas
	,	List:	Ventas.List
	,	name:	'Ventas'
	,	feathersService: ventasService
	,	algebra: Ventas.algebra
	}
);


tag('ventas-model', Ventas.connection);

export default Ventas;
	setup: function(){
		this.connection = connect([localStorage],{
			name: "todos"
		});
		this.connection.clear();
	}
Example #3
0
QUnit.test("getInstance and getData", function(){
	stop();
	var firstData =  {id: 0, foo: "bar"};
	var secondData = {id: 0, foo: "BAR"};
	
	var state = testHelpers.makeStateChecker(QUnit,["cache-getData-empty",
		"base-getData",
		"cache-updateData",
		"connection-foundOne",
		"connection-getInstance-2",
		"cache-getData-item",
		
		"connection-foundOne-2",
		"base-getData-2",
		"cache-updateData-2",
		"updatedInstance"] );
		

	var cacheConnection = connect([function(){
		var calls = 0;
		return {
			getData: function(){
				// nothing here first time
				if(state.get() === "cache-getData-empty") {
					state.next();
					return testHelpers.asyncReject();
				} else {
					state.check("cache-getData-item");
					return testHelpers.asyncResolve(firstData);
				}
			},
			updateData: function(data) {
				if(state.get() === "cache-updateData") {
					state.next();
					deepEqual(data,firstData, "updateData items are right");
					return testHelpers.asyncResolve();
				} else {
					//debugger;
					deepEqual(data,secondData, "updateData 2 items are right");
					state.check("cache-updateData-2");
					return testHelpers.asyncResolve();
				}
			}
		};
	}],{});
	
	var base = function(base, options){
		var calls = 0;
		return {
			getData: function(){
				if(state.get() === "base-getData") {
					state.next();
					return testHelpers.asyncResolve({id: 0, foo: "bar"});
				} else {
					//debugger;
					state.check("base-getData-2");
					return testHelpers.asyncResolve({id: 0, foo: "BAR"});
				}
			}
		};
	};
	var updater = function(){
		return {
			updatedInstance: function(instance, data){
				state.check("updatedInstance");
				deepEqual( data,secondData );
				start();
			}
		};
	};
	
	var connection = connect([base, "constructor","fall-through-cache","constructor-store", "data-callbacks",updater],{
		cacheConnection: cacheConnection
	});
	
	// first time, it takes the whole time
	connection.get({id: 0}).then(function( instance ){
		state.check("connection-foundOne");
		deepEqual( instance, {id: 0, foo: "bar"} );
		setTimeout(secondCall, 1);
	}, testHelpers.logErrorAndStart);
	
	function secondCall() {
		state.check("connection-getInstance-2");
		connection.get({id: 0}).then(function(instance){
			state.check("connection-foundOne-2");
			deepEqual( instance, {id: 0, foo: "bar"}  );
		}, testHelpers.logErrorAndStart);
	}
	
});
Example #4
0
QUnit.test("basics", function(){
	stop();
	var firstItems = [ {id: 0, foo: "bar"}, {id: 1, foo: "bar"} ];
	var secondItems = [ {id: 1, foo: "BAZ"}, {id: 2, foo: "bar"} ];
	
	var state = testHelpers.makeStateChecker(QUnit,["cache-getListData-empty",
		"base-getListData",
		"cache-updateListData",
		"connection-foundAll",
		
		
		"connection-getList-2",
		"cache-getListData-items",
		"connection-foundAll-2",
		"base-getListData-2",
		"cache-updateListData-2",
		"updatedList"] );
		

	var cacheConnection = connect([function(){
		var calls = 0;
		return {
			getListData: function(){
				// nothing here first time
				if(state.get() === "cache-getListData-empty") {
					state.next();
					return testHelpers.asyncReject();
				} else {
					state.check("cache-getListData-items");
					return testHelpers.asyncResolve({data: firstItems.slice(0) });
				}
			},
			updateListData: function(data, set) {
				if(state.get() === "cache-updateListData") {
					state.next();
					deepEqual(set,{},"got the right set");
					deepEqual(data.data,firstItems, "updateListData items are right");
					return testHelpers.asyncResolve();
				} else {
					deepEqual(data.data,secondItems, "updateListData 2 items are right");
					state.check("cache-updateListData-2");
					return testHelpers.asyncResolve();
				}
			}
		};
	}],{});
	
	var base = function(base, options){
		var calls = 0;
		return {
			getListData: function(){
				if(state.get() === "base-getListData") {
					state.next();
					return testHelpers.asyncResolve({data: firstItems.slice(0) });
				} else {
					state.check("base-getListData-2");
					return testHelpers.asyncResolve({data: secondItems.slice(0) });
				}
			}
		};
	};
	var updater = function(){
		return {
			updatedList: function(list, updated){
				state.check("updatedList");
				deepEqual( updated.data.map(getId), secondItems.map(getId) );
				start();
			}
		};
	};
	
	var connection = connect([base, "constructor","fall-through-cache","constructor-store", "data-callbacks",updater],{
		cacheConnection: cacheConnection
	});
	
	// first time, it takes the whole time
	connection.getList({}).then(function( list ){
		state.check("connection-foundAll");
		deepEqual( list.map(getId), firstItems.map(getId) );
		setTimeout(secondCall, 1);
	}, testHelpers.logErrorAndStart);
	
	function secondCall() {
		state.check("connection-getList-2");
		connection.getList({}).then(function(list){
			state.check("connection-foundAll-2");
			deepEqual( list.map(getId), firstItems.map(getId) );
		}, testHelpers.logErrorAndStart);
	}
	
	
	
	// second time, it should return the original list from localStorage
	
	// but then update the list as the request goes out
	
});
Example #5
0
var connect = require("can-connect");
var dataWorker = require("can-connect/data/worker/");


connect([dataWorker, {
	getListData: function(){
		return Promise.resolve({data: [{id: 1},{id: 2}]});
	},

	updateListData: function(){},
	getSets: function(){},
	clear: function(){},

	getData: function(){
		return Promise.resolve({id: 3});
	},

	createData: function(){
		return Promise.resolve({id: 4, name: "createData"});
	},
	updateData: function(){
		return Promise.resolve({id: 5, name: "updateData"});
	},
	destroyData: function(){
		return Promise.resolve({id: 6, name: "destroyData"});
	}
}],{
	name: "todos"
});
Example #6
0
QUnit.asyncTest("basics", function(){

	var Team = DefineMap.extend({
		id: 'string'
	});

	connect([constructor, constructorStore, canMap, canRef, {
		getData: function() {
			return Promise.resolve({ id: 3, name: "Bears" });
		}
	}],
	{
		Map: Team
	});

	var Game = DefineMap.extend({
		id: 'string',
		teamRef: {type: Team.Ref.type},
		score: "number"
	});

	connect([constructor, constructorStore, canMap, canRef,
	{
		getListData: function() {
			return Promise.resolve({data: [
	 			{id: 1, score: 50, teamRef: 2},
				{id: 2, score: 100, teamRef: 3},
				{id: 3, score: 200, teamRef: 2}
	 		]});
	 	},
		getData: function(params) {
			return Promise.resolve({
				id: 1,
				score: 50,
				teamRef: {id: 2, name: "Cubs"}
			});
		}
	}],
	{
		Map: Game
  	});

	var handler = function(){};
	Game.get({id: 1, populate: "teamRef"}).then(function(game){
		game.on("teamRef", handler);
		game.teamRef.on("value", handler);
		var teamRef = game.teamRef;
		QUnit.ok( teamRef.value instanceof Team);
		QUnit.equal(teamRef.value.name, "Cubs");
		QUnit.equal(teamRef.id, 2);

		Game.getList({}).then(function(games){
			QUnit.ok( games[0].teamRef === teamRef, "same team ref");
			QUnit.ok( games[2].teamRef === teamRef, "same team ref on a different object");
			QUnit.ok( teamRef.value instanceof Team);
			QUnit.equal(teamRef.id, 2);
			QUnit.equal(teamRef.value.name, "Cubs");
			QUnit.equal(games[1].teamRef.id, 3);

			QUnit.equal(games[0].teamRef.isResolved(), true);

			games[1].teamRef.on("value", function(ev, newVal){
				QUnit.ok(newVal instanceof Team);
				QUnit.equal(newVal.name, "Bears");
				QUnit.start();
			});

			QUnit.equal(games[1].teamRef.isResolved(), false);

		});
	}, function(error){
		QUnit.ok(false, "error");
		QUnit.start();
	});
});
		}
	},
	isAdmin: function(){
		return this.attr("user") && this.attr("user").attr("isAdmin");
	}
});
Session.List = can.List.extend({Map: Session},{});

var sessionConnection = connect([
	"constructor",
	"can-map",
	"constructor-store",
	"data-callbacks",
	"data-parse",
	"data-url",
	"constructor-callbacks-once"],{
		
	url: {
		getData: "/services/session",
		createData: "/services/session",
		destroyData: "/services/session"
	},
	Map: Session,
	List: Session.List,
	name: "session",
	ajax:
	$.ajax

});

module.exports = Session;
Example #8
0
  'constructor-store',
  'data-callbacks',
  'data-combine-requests',
  'data-inline-cache',
  'data-parse',
  'data-url',
  'constructor-callbacks-once'
];

export const Session = can.Map.extend('Session', {
  define: {}
});

Session.List = can.List.extend({
  Map: Session
}, {});

export const sessionConnection = connect(behaviors, {
  parseInstanceProp: 'data',
  url: {
    createData: data => feathers.authenticate(data),
    destroyData: data => feathers.logout(data)
  },
  idProp: '<%= idProp %>',
  Map: Session,
  List: Session.List,
  name: 'session'
});

export default Session;
Example #9
0
		}
	}
});

Session.List = DefineList.extend({
	'#': Session
});

Session.connection = connect(
	[
		feathersSession,
		construct,
		canMap,
		canRef,
		constructStore,
		dataCallbacks,
		dataParse,
		realtime,
		callbacksOnce
	],
	{
		feathersClient,
		Map: Session,
		List: Session.List,
		name: 'session',
	    queryLogic: new QueryLogic(Session, feathersQuery)
	}
);

export default Session;