Giter Site home page Giter Site logo

angular-dpd's Introduction

angular-dpd

A plugin for Angular that allows for easy interaction with deployd. Usage is the same as the default dpd.js file, but requires configuration of the necessary collections. Extra 'save' helper function added that will POST if the object has no 'id' and will put if it does. Also includes a cache for your objects that have been retrieved for easy insertion into the DOM. This allows you to quickly wire up your angular app with deployd and keep all of your files static so the deployd api can spend it's time serving data instead of a dynamic .js file.

Example usage

var app = angular.module('myApp',['dpd']);

// Configuration:
app.value('dpdConfig',['categories']);
// or
app.value('dpdConfig', { 
	collections: ['categories'], 
	serverRoot: 'http://someotherserver.com/', // optional, defaults to same server
	socketOptions: { reconnectionDelayMax: 3000 }, // optional socket io additional configuration
	useSocketIo: true, // optional, defaults to false
	noCache: true // optional, defaults to false (false means that caching is enabled, true means it disabled)
});


app.controller('bodyController',function($scope, dpd){

	dpd.categories.get();
	
	dpd.categories.get('414b9c5cc315485d');
	
	// Example with an [advanced query](http://docs.deployd.com/docs/collections/reference/querying-collections.md#s-Advanced%20Queries-2035):
	dpd.categories.get($sort: {name: 1}, $limit: 10, rightsLevel: {$gt:0}};
	
	// Promises are supported:
	dpd.categories.post({"value":"cat1","typeId":"987ad2e6d2bdaa9d"})
		.success(function (result) {
			// use result
		})
		.error(function (err) {
			// on error
		})
		.finally(function () {
			// finally
		});
	
	dpd.categories.put('414b9c5cc315485d',{"value":"cat123"});
	
	dpd.categories.del('414b9c5cc315485d');
	
	// You can also use a callback instead of using promises if you prefer:
	dpd.categories.save({"value":"save POST","typeId":"987ad2e6d2bdaa9d"},function(result){
	
		result.value = "save PUT";
		
		dpd.categories.save(result);
	});

});

Cache

Unless caching is disabled via configuration, the dpd object comes with a cache object that will persist after calling a get().

// will return every category that is currently in the cache
dpd.categories.cache.all 

// will return a specific category from the cache
dpd.categories.cache.get('414b9c5cc315485d') 

// will fetch a single category from the database and if it's in the cache, update the cached item.
// If it's not in the cache it will be added.
dpd.categories.get(id,function(result){ ... });

// will add a new category to the the database and on success will add it to the cache
dpd.categories.post({..},function(result){ ... });

// will update a category in the the database and on success will update it in the cache 
// If it's not in the cache it will be added.
dpd.categories.put(id, {..},function(result){ ... });

// will delete a category from the the database and on success will delete it from the cache
dpd.categories.del(id,function(){ ... });

// login a user
dpd.users.exec('login', { username: 'user', password: 'pass' }).success(function(session) { }).error(function(err) { });

Here is an example where you can query objects from the table and immediately put them on the screen.

<!DOCTYPE html>
<html ng-app="myApp">
	<head>
		<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script>
		<script type="text/javascript" src="angular-dpd/angular-dpd.js"></script>
		<script type="text/javascript" src="app.js"></script>
	</head>
	<body ng-controller="bodyController">
		<ul ng-init="dpd.categories.get()">
			<li ng-repeat="c in dpd.categories.cache.all">
				{{c.value}}
			</li>
		</ul>
	</body>
</html>

Socket.IO

If socket.io is enabled in the configuration, it can be used like this:

app.controller('bodyController',function($scope, dpd){
	dpd.categories.on($scope, "changed", function (result) { // this handles "categories:changed"
		console.log("event fired");
	}
	// or
	dpd.on($scope, "categories:changed", function (result) {
		console.log("event fired");
	}
}

For low-level access, the raw socket is exposed as dpd.socket.rawSocket.

Note: We inject $scope in the call so that the library can automatically remove the events if the $scope is $destroyed (such as when a route change occurs).

dpd.users

The dpd.users collection traditionally allows access to several functions including dpd.users.me(), dpd.users.login, dpd.users.logout. These are currently not well supported, but may be used as follows:

##dpd.users.me() use dpd.users.get('me') - this seems to work as long as your sid cookie is set

##dpd.users.login use:

function login(user){
    $http.post( dpdConfig.serverRoot + 'users/login', { username: user.username, password: user.password})
      .then(
      function(session, error) {
        if (error) {
          alert(error.message);
          return false;
        } else {
          $cookies.sid = session.data.id; // set the sid cookie
          $state.go('loginSuccess');
        }
      });
  }

dpd.users.logout()

Use dpd.users.get('logout')

angular-dpd's People

Contributors

andreialecu avatar fotoflo avatar iranreyes avatar slively avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

angular-dpd's Issues

The user that not include socket.io can't use the script

If a user will not use socket.io, then he has to desactivate it in the 'dpdConfig', but when enter to the definition of dpdSocket Factory, the, the execution enter inside the first if and return undefined. This must be throw an error because we are defining here a factory and we have to return always an object. So a solution here is return a empty object, instead undefined.

I created a PR to this issue.

Rename to angular-dpd

Hey there,

Since that recent PR, this is not only about caching, so I think renaming this to angular-dpd would be a good idea.

It's not a dpd extension, those start with dpd-* , it's an angular thing, so it makes sense to start with angular-.

I already renamed it in my repo, so I wanted to sync up. :)

dpd.users.logout and dpd.users.exec('logout') both fail.

dpd.users.logout and dpd.users.exec('logout') both fail.

The former is undefined and the latter:

dpd.users.exec('logout') gets
POST http://localhost:2403/users/logout 400 (Bad Request)

Also tried adding the following function to the library with no success (get a 404 error)

            dpd[d].logout = function(s, e) {
                return $http.post(serverRoot + '/logout', null, { withCredentials: true }).success(function(data, status, headers, config) {
                  return;
                }).success(checkUndefinedFunc(s)).error(ef).error(checkUndefinedFunc(e));
            };

firebase approach

Hi, would it be an idea to maybe go towards a firebase approach where you can sync an object or array that automatically would get updated in the $scope, instead of having to push the results?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.