diff --git a/README.md b/README.md index 7a239e5..8b0e3bb 100755 --- a/README.md +++ b/README.md @@ -1,37 +1,124 @@ -Master Branch: [](https://travis-ci.org/gerard2p/ember-cli-crudtable) +# CRUD Table [](https://travis-ci.org/gerard2p/ember-cli-crudtable) [](https://www.npmjs.org/package/ember-cli-crudtable) [](https://www.npmjs.org/package/ember-cli-crudtable) +This addon allows you to easly create a CRUD Table, it will take you only 5s!. +___ -Development Branch: [](https://travis-ci.org/gerard2p/ember-cli-crudtable) -# Hello Every One -I've didn't notice that people was watching this proyect, so i'm sorry for the lack of documentation. -Today (24 March, 2015) I just finished one version wich is working, so hang on a second and tomorrow I'll update the docuemntation. +##Development [](https://travis-ci.org/gerard2p/ember-cli-crudtable) +___ -If you could support me by following me, or maybe by letting any comment on the issue section It'll be great. +##Installation +``` +ember install:addon ember-cli-crudtable +``` -Thank You :) +##How to use it +You can use the helper **{{crud-table}}** there are some minimun variables you must specify and these are: +###Minimun Configuration +1. class **The class name you want the component to have** +2. fields **The field names you want the component to render separated by ","** -# Ember-cli-crudtable +###Action Configuration +These variables are completed optional, if you're using the integrated mixin for the controller. -This README outlines the details of collaborating on this Ember addon. +1. createRecord: [default: '*create*'] +1. updateRecord: [default: '*update*'] +1. deleteRecord: [default: '*delete*'] +1. cancelRecord: [default: '*cancel*'] + 2. This actions is triggered when click on **"cancel"**, the default configuration makes a rollback on the record or deletes it if new. +1. getRecord: [default: '*getRecord*'] + 2. This action creates a new empty record. +1. searchRecord: [default: '*FetchData*'] + 2. This actions if the one witch searches for your records. -## Installation +###Style Configuration +1. stripped: [ true| **false** ] - Makes the table to render stripped. +1. hover: [ true | **false** ] - Allows to hover the table. -* `git clone` this repository -* `npm install` -* `bower install` +##Controller Configuration +You can use this very same code when creating your controller and the only thing you have to worry about is to indicate the model. -## Running +``` +import CTABLE from 'ember-cli-crudtable/mixins/crud-controller'; +import Ember from 'ember' -* `ember server` -* Visit your app at http://localhost:4200. +var CrudTable = CTABLE('school'); +export default Ember.ObjectController.extend(CrudTable); +``` -## Running Tests -* `ember test` -* `ember test --server` +##Full Example +``` +//app/router.js +import Ember from 'ember'; +import config from './config/environment'; -## Building +var Router = Ember.Router.extend({ + location: config.locationType, + rootURL: config.baseURL +}); -* `ember build` +Router.map(function () { + this.resource('schools'); +}); -For more information on using ember-cli, visit [http://www.ember-cli.com/](http://www.ember-cli.com/). +export default Router; + +``` + +``` +//app/models/school.js +import DS from 'ember-data'; + +var attr = DS.attr; + +export default DS.Model.extend({ + Name: attr('string'), + City: attr('string'), + Adress: attr('string'), + CP: attr('string'), + Responsable:attr('string'), + AmountStudents:attr('number') +}); + +``` + +``` +//app/controllers/schools/index.js +import CTABLE from 'ember-cli-crudtable/mixins/crud-controller'; +import Ember from 'ember' + +var CrudTable = CTABLE('school'); +export default Ember.ObjectController.extend(CrudTable); +``` + + +``` +//app/templates/schools/index.hbs +{{crud-table +fields="Name,City,Adress" +deleteRecord='delete' +updateRecord='update' +createRecord='create'}} +``` + +Thats All you can have now a fully CRUD table that communicates with your server and allows pagination. + + +##Pagination +The pagination data must be included in the meta response of your server so ember-data can use it and crudtable has access to it. + +This is a sample meta: + +``` +{ + "meta":{ + "next":2, + "previous"":null, + "count":20 + } +} +``` + +##Upcoming +I'm in a little hurry but as soon as posible i'll be adding more features and also providing a more detailed doc about the functions. +But try it, is quite simple to have your fully functional CRUD application. \ No newline at end of file diff --git a/addon/components/crud-table.js b/addon/components/crud-table.js index b6cc09e..b96f36e 100755 --- a/addon/components/crud-table.js +++ b/addon/components/crud-table.js @@ -8,7 +8,57 @@ var CustomField = Ember.Object.extend({ Type: null, listener: function () {}.observes('Value') }); +var recalculatePagination = function (that, meta) { + var arr = []; + var tpages = Math.ceil(meta.total / meta.showing); + var neightboor = 3; + var slots = 1; + var max = neightboor * 2 + slots * 2 + 3; + var de1 = slots; + var de2 = meta.current - neightboor; + var df2 = tpages - slots + 1; + var df1 = meta.current + neightboor; + var compress = tpages > max; + var preadd = true; + var postadd = true; + for (var i = 1; i <= tpages; i++) { + if (compress) { + var TP = max - ((tpages - meta.current) + neightboor + 1 + slots + 1); + var TP2 = max - (meta.current + neightboor + 1 + slots); + if ((de1 < i && i < de2) && i < de2 - TP) { + if (preadd) { + preadd = false; + arr.push({ + page: "..", + current: false + }); + } + } else if ((df1 < i && i < df2) && i > df1 + TP2) { + if (postadd) { + postadd = false; + arr.push({ + page: "..", + current: false + }); + } + } else { + arr.push({ + page: i, + current: meta.current === i + }); + } + } else { + arr.push({ + page: i, + current: meta.current === i + }); + } + } + + meta.links = arr; + that.set('pagination', meta); +}; var regenerateView = function (cmp) { var ComplexModel = []; if (cmp.value) { @@ -36,29 +86,88 @@ var regenerateView = function (cmp) { var showmodal = function () { $("#CrudTableDeleteRecordModal").modal('show'); }; +var metadata = function (records, that) { + var inflector = new Ember.Inflector(Ember.Inflector.defaultRules); + var meta = records.get("meta"); + meta = { + total: meta.count, + previous: meta.previous, + current: meta.previous ? (meta.next ? meta.next - 1 : meta.previous + 1) : 1, + next: meta.next, + showing: that.page_size, + name: inflector.pluralize(records.type.typeKey) + }; + meta.from = (meta.current - 1) * meta.showing + 1; + meta.to = meta.current * meta.showing; + meta.to = meta.to > meta.total ? meta.total : meta.to; + recalculatePagination(that, meta); +}; var hidemodal = function () { $("#CrudTableDeleteRecordModal").modal('hide'); }; +var lastquery = { + page: null +}; -export default Ember.Component.extend({ - attributeBindings: ['style'], - style: function () { - return 'color: ' + this.get('name') + ';'; - }.property('name'), +export default Ember.Component.extend({ stripped: false, hover: false, - createRecord: '', - updateRecord: '', - deleteRecord: '', + createRecord: 'create', + updateRecord: 'update', + deleteRecord: 'delete', cancelRecord: 'cancel', + searchRecord: 'FetchData', currentRecord: null, getRecord: 'getRecord', + isLoading: true, + SearchTerm: "", + SearchField: "", + value: [], + layout: layout, + class: "", + fields: "id", actions: { + goto: function (page) { + var that = this; + var deferred = Ember.RSVP.defer('crud-table#goto'); + lastquery.page = page; + that.set('isLoading', true); + this.sendAction('searchRecord', lastquery, deferred); + deferred.promise.then(function (records) { + metadata(records, that); + that.set('value', records); + regenerateView(that); + that.set('isLoading', false); + }, function (data) { + alert(data.message); + that.set('isLoading', false); + }); + }, + internal_search: function () { + var field = $("#SearchField").val(); + var query = {}; + var that = this; + query[field] = this.get('SearchTerm'); + lastquery = query; + var deferred = Ember.RSVP.defer('crud-table#createRecord'); + that.set('isLoading', true); + this.sendAction('searchRecord', query, deferred); + deferred.promise.then(function (records) { + metadata(records, that); + that.set('value', records); + regenerateView(that); + that.set('isLoading', false); + }, function (data) { + alert(data.message); + that.set('isLoading', false); + }); + }, confirm: function () { var that = this; var deferred; + this.set('isLoading', true); if (this.get('newRecord')) { deferred = Ember.RSVP.defer('crud-table#createRecord'); this.sendAction('createRecord', this.get('currentRecord').RoutedRecord, deferred); @@ -71,19 +180,36 @@ export default Ember.Component.extend({ this.sendAction('updateRecord', this.get('currentRecord').RoutedRecord, deferred); } } + + var updateview = Ember.RSVP.defer('crud-table#pagination'); deferred.promise.then(function () { + lastquery.page = that.get('pagination').current; + that.sendAction('searchRecord', lastquery, updateview); + //regenerateView(that); + //recalculatePagination(that,that.get('pagination')); + }, function (data) { + alert(data.message); + that.set('isLoading', false); + }); + + updateview.promise.then(function (records) { + metadata(records, that); + that.set('value', records); regenerateView(that); hidemodal(); + that.set('isLoading', false); }, function (data) { alert(data.message); + that.set('isLoading', false); }); }, internal_create: function () { var that = this; that.set('newRecord', true); var deferred = Ember.RSVP.defer('crud-table#newRecord'); - this.sendAction('getRecord', deferred); - deferred.promise.then(function ( /*record*/ ) { + that.sendAction('getRecord', deferred); + deferred.promise.then(function (record) { + that.get('value').pushObject(record); regenerateView(that); that.set('currentRecord', that.get('ComplexModel').get('lastObject')); showmodal(); @@ -105,10 +231,6 @@ export default Ember.Component.extend({ //this.get('delete')(); } }, - layout: layout, - class: "", - value: [], - fields: "id", init: function () { var that = this; this._super(); @@ -120,19 +242,33 @@ export default Ember.Component.extend({ }.on('willInsertElement'), setup: function () { var that = this; - regenerateView(that); - //Ember.addObserver('value',that,function(){ - // regenerateView(that); - //}); + var deferred = Ember.RSVP.defer('crud-table#createRecord'); + that.set('isLoading', true); + this.sendAction('searchRecord', {}, deferred); + + $(this).addClass(this.get('class')); + + deferred.promise.then(function (records) { + that.page_size = records.get('content.length'); + metadata(records, that); + that.set('value', records); + regenerateView(that); + that.set('isLoading', false); + }, function (data) { + alert(data.message); + that.set('isLoading', false); + }); $("#CrudTableDeleteRecordModal").modal('hide'); $('#CrudTableDeleteRecordModal').on('hidden.bs.modal', function () { var deferred = Ember.RSVP.defer('crud-table#cancelRecord'); + var template = Ember.RSVP.defer('crud-table#RenderTemplate'); that.sendAction('cancelRecord', that.get('currentRecord').RoutedRecord, deferred); deferred.promise.then(function () { regenerateView(that); that.set('newRecord', false); that.set('isDeleting', false); that.set('currentRecord', null); + template.resolve(true); }, function (data) { alert(data); }); @@ -141,6 +277,6 @@ export default Ember.Component.extend({ }.on('didInsertElement'), teardown: function () { - //this._drop.destroy(); + }.on('willDestroyElement'), }); diff --git a/addon/mixins/route.js b/addon/mixins/crud-controller.js similarity index 69% rename from addon/mixins/route.js rename to addon/mixins/crud-controller.js index 69c41fb..0f6a65b 100644 --- a/addon/mixins/route.js +++ b/addon/mixins/crud-controller.js @@ -1,11 +1,12 @@ import Ember from 'ember'; -//import App from '../app'; -//import EmberValidations from 'ember-validations'; export default function (model /*settings*/) { - return Ember.ObjectController.extend({ + return Ember.Mixin.create({ isEditing: false, actions: { + FetchData:function(query,deferred){ + this.store.find(model,query).then(deferred.resolve,deferred.reject); + }, getRecord: function (deferred) { deferred.resolve(this.store.createRecord(model)); }, @@ -18,21 +19,20 @@ export default function (model /*settings*/) { }, update: function (record, deferred) { - record.save().then(deferred.resolve, deferred.reject); - //var self = this; - //var promises = []; + var that = this; + var promises = []; //if(self.model.get('isDirty')){ - //promises.push(record.save()); + promises.push(record.save()); //}else{ // self.set('isEditing',false); //} - /*Ember.A(Ember.keys(record._dependentRelations)).any(function (key) { - var value = Ember.get(self.model, key); + Ember.A(Ember.keys(record._relationships)).any(function (key) { + var value = Ember.get(that.model, key); if (value.get('isDirty')) { promises.push(value.get('content').save()); } - });*/ - //Ember.RSVP.Promise.all(promises).then(deferred.resolve, deferred.reject); + }); + Ember.RSVP.Promise.all(promises).then(deferred.resolve, deferred.reject); }, delete: function (record, deferred) { diff --git a/addon/templates/components/crud-table.hbs b/addon/templates/components/crud-table.hbs index f7de793..690edbd 100755 --- a/addon/templates/components/crud-table.hbs +++ b/addon/templates/components/crud-table.hbs @@ -5,52 +5,52 @@
{{field}} | {{/each}} {{#if this.editdelete}} -Actions | +Actions | {{/if}}
---|---|---|
{{#if this.updateRecord}} - | @@ -59,6 +59,22 @@ {{/each}}