How to use the can-connect.behavior function in can-connect

To help you get started, we’ve selected a few can-connect examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github canjs / can-connect-feathers / service / service.js View on Github external
"use strict";
var connect = require('can-connect');

function getIdProp (Model) {
	var algebraIdProp;
	var algebraClause = Model.algebra && Model.algebra.clauses && Model.algebra.clauses.id;
	if (algebraClause) {
		algebraIdProp = Object.keys(algebraClause)[0];
	}
	if (!algebraIdProp && !Model.idProp) {
		throw new Error('An idProp was not set in the Model for ' + Model + '. Things may not work as expected.');
	}
	return algebraIdProp || Model.idProp;
}

module.exports = connect.behavior('data/feathers-service', function (base) {
	var helpURL = 'https://canjs.com/doc/can-connect-feathers.html';
	if (!this.feathersService) {
		throw new Error('You must provide a feathersService to the feathers-service behavior: ' + helpURL);
	}

	var service = this.feathersService;

	return {
		init: function () {
			base.init.apply(this, arguments);
			var self = this;
			// Connect to real-time events.
			service.on('created', function (message) { self.createInstance(message); });
			service.on('updated', function (message) { self.updateInstance(message); });
			service.on('patched', function (message) { self.updateInstance(message); });
			service.on('removed', function (message) { self.destroyInstance(message); });
github canjs / can-connect / data / callbacks / callbacks.js View on Github external
* @signature `connection.destroyedData(data, params, cid)`
	 *
	 *   Called with the resolved response data
	 *   of [can-connect/connection.destroyData]. The result of this function will be used
	 *   as the new response data.
	 *
	 *
	 *   @param {Object} data The raw data returned by the response.
	 *   @param {Object} params The parameters used to make this request.
	 *   @param {Number} cid The cid of the instance created.
	 *   @return {Object} The raw data this request represents.
	 */
	destroyData: "destroyedData"
};

module.exports = connect.behavior("data/callbacks",function(baseConnection){

	var behavior = {
	};

	// overwrites createData to createdData
	each(pairs, function(callbackName, name){

		behavior[name] = function(params, cid){
			var self = this;

			return baseConnection[name].call(this, params).then(function(data){
				if(self[callbackName]) {
					return self[callbackName].call(self,data, params, cid );
				} else {
					return data;
				}
github canjs / can-connect / constructor / callbacks-once / callbacks-once.js View on Github external
*/
	"updatedInstance",
	/**
	 * @function can-connect/constructor/callbacks-once/callbacks-once.destroyedData destroyedData
	 * @parent can-connect/constructor/callbacks-once/callbacks-once
	 *
	 * Called with the resolved response data
	 * of [can-connect/connection.destroyData]. The result of this function will be used
	 * as the new response data.
	 */
	"destroyedInstance"
];



module.exports = connect.behavior("constructor/callbacks-once",function(baseConnection){

	var behavior = {
	};

	// overwrites createData to createdData
	forEach.call(callbacks, function(name){
		behavior[name] = function(instance, data ){

			var lastSerialized = this.getInstanceMetaData(instance, "last-data-" + name);

			var serialize = sortedSetJSON(data);
			if(lastSerialized !== serialize) {
				var result =  baseConnection[name].apply(this, arguments);
				this.addInstanceMetaData(instance, "last-data-" + name, serialize);
				return result;
			}
github bitovi-components / bit-c3 / bit-c3 / node_modules / can-connect / src / data / localstorage-cache / localstorage-cache.js View on Github external
var indexOf = function(connection, props, items){
	var id = connection.id(props);
	for(var i = 0; i < items.length; i++) {
		if( id == connection.id(items[i]) ) {
			return i;
		}
	}
	return -1;
};

var setAdd = function(set, items, item, algebra){
	return items.concat([item]);
};


module.exports = connect.behavior("data-localstorage-cache",function(baseConnect){

	var behavior = {
		// ## Helpers


		// a map of each id to an instance
		_instances: {},
		getSetData: function(){

			var sets = {};
			var self = this;
			forEach.call((JSON.parse(localStorage.getItem(this.name+"-sets"))|| []), function(set){
				// make sure we actually have set data
				 var setKey = sortedSetJSON(set);

				if( localStorage.getItem(self.name+"/set/"+setKey) ) {
github canjs / can-connect / data / memory-cache / memory-cache.js View on Github external
*   url: "/services/todos",
 *   cacheConnection: cacheConnection
 * });
 * ```
 */
var getItems = require("can-connect/helpers/get-items");
var connect = require("can-connect");
var sortedSetJSON = require("can-connect/helpers/sorted-set-json");
var canSet = require("can-set");
var overwrite = require("can-connect/helpers/overwrite");
var setAdd = require("can-connect/helpers/set-add");
var indexOf = require("can-connect/helpers/get-index-by-id");
var assign = require("can-util/js/assign/assign");
var cloneData = require("can-connect/helpers/clone-data");

module.exports = connect.behavior("data/memory-cache",function(baseConnection){

	var behavior = {
		_sets: {},
		getSetData: function(){
			return this._sets;
		},

		__getListData: function(set){
			var setsData = this.getSetData();
			var setData = setsData[sortedSetJSON(set)];
			if(setData) {
				return setData.items;
			}
		},
		_instances: {},
		getInstance: function(id){
github canjs / can-connect-feathers / session / session.js View on Github external
"use strict";
var connect = require('can-connect');
var errors = require('@feathersjs/errors');
var authAgent = require('feathers-authentication-popups').authAgent;
var decode = require('jwt-decode');
var payloadIsValid = require('../utils/utils').payloadIsValid;
var hasValidToken = require('../utils/utils').hasValidToken;
var convertLocalAuthData = require('../utils/utils').convertLocalAuthData;
var ObservationRecorder = require('can-observation-recorder');
var zoneStorage = require('./storage');

module.exports = connect.behavior('data/feathers-session', function (base) {
	var helpURL = 'https://canjs.com/doc/can-connect-feathers.html';
	var feathersClient = this.feathersClient;

	if (!feathersClient) {
		throw new Error('You must provide a feathersClient instance to the feathers-session behavior. See ' + helpURL);
	}
	if (!this.Map) {
		throw new Error('You must provide a Map instance to the feathers-session behavior. See ' + helpURL);
	}
	if (!feathersClient.authentication) {
		throw new Error('You must register the feathers-authentication-client plugin before using the feathers-session behavior. See ' + helpURL);
	}

	var options = feathersClient.authentication.options;
	var Session = this.Map;
github bitovi-components / bit-c3 / bit-c3 / node_modules / can-connect / src / data / combine-requests / combine-requests.js View on Github external
* var todosConnection = connect(["data-combine-requests","data-url"],{
 *   url: "/todos",
 *   algebra: new Algebra(set.comparators.range("start","end"))
 * });
 * ```
 *
 * Now the following will make single request:
 *
 * ```
 * todosConnection.getListData({start: 0, end: 49})
 * todosConnection.getListData({start: 0, end: 5});
 * todosConnection.getListData({start: 50, end: 99});
 * ```
 *
 */
module.exports = connect.behavior("data-combine-requests",function(base){
	var pendingRequests; //[{set, deferred}]

	return {
		/**
		 * @function can-connect/data/combine-requests.unionPendingRequests unionPendingRequests
		 * @parent can-connect/data/combine-requests.algebra
		 *
		 * @signature `connection.unionPendingRequests( pendingRequests )`
		 *
		 *   @param {Array}
		 *
		 *   An array of objects, each containing:
		 *
		 *   - `set` - the requested set
		 *   - `deferred` - a deferred that will be resolved with this sets data
		 *
github canjs / can-connect / constructor / store / store.js View on Github external
decrement: function(connection){
		pendingRequests--;
		if(pendingRequests === 0) {
			noRequestsTimer = setTimeout(function(){
				requests.dispatch("end");
			},10);
		}
	},
	count: function(){
		return pendingRequests;
	}
};
assign(requests, canEvent);


var constructorStore = connect.behavior("constructor/store",function(baseConnection){

	var behavior = {
		/**
		 * @property {can-connect/helpers/weak-reference-map} can-connect/constructor/store/store.instanceStore instanceStore
		 * @parent can-connect/constructor/store/store.stores
		 *
		 * A store of instances mapped by [can-connect/base/base.id].
		 *
		 * @type {can-connect/helpers/weak-reference-map}
		 *
		 *   Stores instances by their [can-connect/base/base.id] which have had
		 *   [can-connect/constructor/store/store.addInstanceReference] called more
		 *   times than [can-connect/constructor/store/store.deleteInstanceReference].
		 *
		 *   ```js
		 *   connection.addInstanceReference(todo5);
github canjs / can-connect / data / inline-cache / inline-cache.js View on Github external
*     "{\"completed\": true}": {data: [{...},{...}]}
 *   }
 * }
 * 
 * ```
 *
 * Now, the first time the following requests are made, the cached data will be used:
 *
 * ```
 * todosConnection.get({id: 1})
 * todosConnection.getList({completed: true})
 * ```
 *
 *
 */
module.exports = connect.behavior("data/inline-cache",function(baseConnect){

	/**
	 * @property {Object} can-connect/data/inline-cache/inline-cache.INLINE_CACHE INLINE_CACHE
	 * @parent can-connect/data/inline-cache/inline-cache.globals
	 *
	 * Contains response data for requests that should not be made.
	 *
	 * @option {Object>}
	 *
	 *   A mapping of a [connection.name] to an Object that contains the
	 *   cached data for that connection.  That inner object is a mapping
	 *   between either [can-connect/base/base.id ids] or [can-connect/base/base.listSet serialized sets] to
	 *   response data for those requests.
	 *
	 *
	 */
github canjs / can-connect / can / merge / merge.js View on Github external
var smartMerge = require('can-connect/helpers/map-deep-merge');
var connectMap = require('can-connect/can/map/map');
var canBatch = require('can-event/batch/batch');
var connect = require("can-connect");

module.exports = connect.behavior("can/merge",function(baseConnection){
	return {
		/**
		 * @function can-connect/can/merge/merge.createdInstance createdInstance
		 * @parent can-connect/can/merge/merge.instance-callbacks
		 *
		 * @description Makes minimal updates to an instance's properties and
		 * its nested properties using [can-connect/helpers/map-deep-merge].
		 *
		 * @signature `connection.createdInstance(instance, props)`
		 *
		 *   Calls `mapDeepMerge` and triggers the `'created'` event on the instance
		 *   within a [can-event/batch/batch batch].
		 *
		 *   @param {can-connect/Instance} instance The instance that was just created whose
		 *   properties will be updated.
		 *   @param {Object} props The new data the instance and children of the