Coder Social home page Coder Social logo

emberize's Introduction

Laravel 4 response generator for Ember.

Emberize in a nutshell

1. Usage: Emberize::make($model or $collection)
2. Sideload,embed,links
2. (Not Yet)Polymorphic relationships. 
3. Public keys(GUID, email, username, etc as id)
4. Change attributes,relationships dynamically(If authorized then include user relationship in json)         

Installation

1. Add the following to your composer.json "require" array 
	
    "breadam/emberize": "dev-master"

2. Add the following to your app/config/app.php "providers" array

    "Breadam\Emberize\EmberizeServiceProvider"

3. Update composer
    
    composer update

4. Publish config file 
    
    php artisan config:publish breadam/emberize

5. Edit app/config/packages/breadam/emberize/config.php to your needs

Quick Example

-- Database --

table foos(id,public_key,name,bar_id)

table bars(id,bar_specific_public_key,name)
    
table buses(id,public_key,name,foo_id)

-- Models --
    
    class Foo extends Eloquent{

        public function bar(){ return $this->belongsTo("Bar"); }
        
        public function buses(){ return $this->hasMany("Bus"); }
    }
    
    class Bar extends Eloquent{
        public function foos(){ return $this->hasMany("Foo"); }
    }
    
    class Bus extends Eloquent{
        public function foo(){ return $this->belongsTo("Foo"); }
    }
    
-- app/config/packages/breadam/emberize/config.php --

return array(
    
	"mode" => null,

    "identifier" => array(
        "key" => "id",
        "value" => "public_key"
    ),
    
    "resources" => array(
    
        "foo" => array(
            "fields" => array(
				"name",
				"bar:sideload",
				"buses:"embed"
			)
        ),
        
        "bar" => array(
            
            "identifier" => array(
                "value" => "bar_specific_public_key"
            ),
            
            "fields" => array(
				"name",
				"foos:links"
			)
        ),
        
        "bus" => array(
            "fields" => array(
				"name",
				"foo:sideload"
			)
        )
    )
)

-- Basic usage  --
    
    Route::get('/', function(){
    
        $foo = Foo::find(1);
        
        return Emberize::make($foo); 
    });
    
-- Update fields. Change will persist until the end of request. --

    Route::get('/', function(){
    
        $foo = Foo::find(1);
        
        Emberize::fields(array(
            "foo" => array(
                "exclude" => "bar"
            )
        ));
        
        return Emberize::make($foo); 
    });

-- Update fields.     --

    Route::get('/', function(){
                
        $foo = Foo::find(1);
        
        Emberize::fields(array(
           "bar" => array(
               "exclude" => array("foos")
           )
        ));
        
        if($someCondition){
        
            return Emberize::make($foo,array(
                "foo" => array(
                    "exclude" => array("buses")
                )
            )); 
            
        }else{
            return Emberize::make($foo,array(
                "foo" => array(
                    "exclude" => array("name")
                ),
                "buses" => array(
                    "exclude" => array("foos")
                )
           );
        }
    });

Configuration

mode:

Set default mode. If "null", Emberize will prepare only primary keys of relationships    

value: null,sideload,embed,link
default: null

identifier:

If set, Emberize will use "key" as the primary key name and "value" attribute as primary key value. If not set, Emberize will use $model->getKeyName() and $model->getKey()   

value: 	

	"identifier" => array(
    	"key" => string,
    	"value" => string
	)

default:

	"identifier" => array(
    	"key" => $model->getKeyName(),
    	"value" => $model->getKey()
	)

resources:

"resources" => array(
	
	"resource_1" => array(
		"identifier" => array( "key" => "...","value" => "..."),
		"fields" => array(
			"attribute_1",
			"relationship_1:mode"
		)
	),
	"resource_2" => array(...)
)

// Missing

resolver:

// Missing

value: A class name implementing "Breadam\Emberize\ResourceNameResolverInterface"

value: "Breadam\Emberize\DefaultResourceNameResolver"   

Methods

Emberize::make([$model|$collection],array $fields = null)

'$fields': will be merged with fields defined in config and with Emberize::fields(...).

Emberize::fields(array $fields,$merge = false)

'$fields': will be merged with fields defined in config. 
 
usage: 

	Emberize::fields(array(
  "model_name_1" => array(
			"include" => array("field_name_1","field_name_2",...)
			"exclude" => array("field_name_1","field_name_2",...)
   ),
		"model_name_2" => array(
			"include" => array("field_name_1","field_name_2",...)
			"exclude" => array("field_name_1","field_name_2",...)
  )
	));

emberize's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

emberize's Issues

Hi, this looks interesting

I'm new to emberjs, and looking forward to create an app with it and laravel.
If I'm not mistaken, this is supposed to be used with 'ember data" right ?
I'm also planning on using polymorphic relationships. But still can wrap my head around it with ember, does emberize support polymorphic relationships?

And the last question would be, how is this differs from https://github.com/rtablada/eloquent-ember ? Do they serve the same purpose ?

Model name on non models

My models are in a dsl folder, and are all 'Entity' classes, which obviously causes issues.

To get around this i have used a var called modelName on my model and checked for it in the modelName function as a hack, but i am happy to make a more extensible system if you are interested.

I propose that the variable name be added to the config so people can customise it, and then add then add the check to model name.

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.