Skip to content

Instantly share code, notes, and snippets.

@visualjeff
visualjeff / gist:3d7d06be88d777153f61228d29c106d9
Created May 14, 2016 17:35
ember-data override to rev attributes when null. data source (couchdb) doesn't want a null rev attribute when adding (POSTing) a record.
//Override is to accomidate couchdb and that it doesn't like like to see a revision or rev
//when creating a record. But because our model contains a rev (which is used for updates
//or patch request) we need to make sure during a POST request that we cut or remove the rev
//property out of the payload. Since Ember-Data sets it to null (record is about to be added)
//removing it won't hurt a thing.
serialize(snapshot, options) {
let data = this._super(...arguments);
if (data.data.attributes.rev === null) {
delete data.data.attributes.rev;
@visualjeff
visualjeff / gist:fd0707201fe0e97dcaac1e719354425e
Created May 14, 2016 17:45
Ember-data override to pull out meta data from a particular data type. Note this is a findAll override. So we're pulling back a collection of locations.
normalizeFindAllResponse(store, primaryModelClass, payload, id, requestType) {
if (primaryModelClass.modelName === 'location') { // My data model in this case is location.
if (payload && payload.hasOwnProperty('meta')) {
let meta = payload.meta;
//Store / persist or do something your meta data.
delete payload.meta;
}
}
return this._super(...arguments);
}
@visualjeff
visualjeff / gist:ffdf0eaa1ba02104be47664e79a84865
Created May 14, 2016 20:57
Ember-data JSONAPIAdapter override to append rev to delete requests for couchdb.
urlForDeleteRecord(id, modelName, snapshot) {
return this._super(...arguments) + `?rev=${snapshot._attributes.rev}`;
}
@visualjeff
visualjeff / gist:aefec63d012873b2b8032810c2730ba6
Created May 14, 2016 20:59
Ember-data JSONAPIAdapter override to address case issues. Camel-case versus kabab-case.
pathForType: function(type) {
type = this._super(...arguments);
if (type === 'building-types') {
type = type.camelize(); //Node API is expects a camelized path for this api signature. i.e., buildingTypes
}
return type;
},
@visualjeff
visualjeff / gist:d27392719fd96955ae006865f95b9e83
Created June 30, 2016 22:07
How to add code complete for Node.js in Visual Studio Code.
sudo npm install typings --global
typings search --name node
typings install env~node --global --save
Then I added the following to the top of my index.js file.
/// <reference path="typings/index.d.ts"/>
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 50px;
NOTE: While this is an easy fix. Its doesn't work so well with Ember.
Also there are trade offs. Mobiles apps will be forced to scroll out of the gate.
import Ember from 'ember';
export default Ember.Component.extend({
didInsertElement: function() {
this.set('options', {
// the element holding pannable content area
contentEl: Ember.$(this.element).find('.ptr-content')[0],
// the element holding pull to refresh loading area
ptrEl: Ember.$(this.element).find('.ptr-indicator')[0]
Mirage cheatsheet:
1. Install Mirage: ember install ember-cli-mirage
2. Set namespace in mirage/config.js
3. Set route handler in mirage/config.js
4. Generate mirage model. ember g mirage-model <model_name>
5. If you not using JSONAPI. Customize your mirage serializer
6. Seed the database: ember g mirage-fixture <fixture_name>
7. Remove senarios directory. It prevents fixtures being load. rm -rf senarios
8. Refactor to use shorthands where possible.
@visualjeff
visualjeff / gist:a37ea04650e8a4de2a3a8953f09b0cd4
Created July 30, 2016 21:56
Javascript's Higher Order Functions and Ember's Higher Order functions
Higher order functions:
.forEach() - Iterates over a set
.map() - Projects a set
.filter() - Narrows down a set based on the predicate
.find() - Returns the first match in a set based on the predicate
.any() - Returns true if match is found based on the predicate
.every() - Returns true if match is found for all elements in the set (predicate also)
Stop using switch, for and while loops!
@visualjeff
visualjeff / gist:f6ebbe05d24939a71caddbbaab218ff2
Last active July 30, 2016 22:16
Ember-Data Serializer Notes
Ember Data Notes:
Serializers
Either you are:
1. deserialization - data from the server, extract then normalize
2. serialization - Sending data to the server
You can define an alternate primary key in the serializer: