Skip to content

Instantly share code, notes, and snippets.

@rlivsey
rlivsey / test_channel.ex
Last active July 22, 2016 10:30
Subscribing to events from a Phoenix.Channel in 1.2.0-rc.0
defmodule MyApp.TestChannel do
use MyApp.Web, :channel
require Logger
intercept ["some-event"]
def join("test:lobby", _, socket) do
# subscribe to a topic
subscribe socket, "some-topic"
- @ ~/Development/ember-addons/mobiledoc/mobiledoc-kit[atoms] $ npm link mobiledoc-html-renderer
/Users/rlivsey/Development/ember-addons/mobiledoc/mobiledoc-kit/node_modules/mobiledoc-html-renderer -> /usr/local/lib/node_modules/mobiledoc-html-renderer -> /Users/rlivsey/Development/ember-addons/mobiledoc/mobiledoc-html-renderer
- @ ~/Development/ember-addons/mobiledoc/mobiledoc-kit[atoms] $ npm link mobiledoc-text-renderer
/Users/rlivsey/Development/ember-addons/mobiledoc/mobiledoc-kit/node_modules/mobiledoc-text-renderer -> /usr/local/lib/node_modules/mobiledoc-text-renderer -> /Users/rlivsey/Development/ember-addons/mobiledoc/mobiledoc-text-renderer
- @ ~/Development/ember-addons/mobiledoc/mobiledoc-kit[atoms] $ npm run build
> mobiledoc-kit@0.7.0 build /Users/rlivsey/Development/ember-addons/mobiledoc/mobiledoc-kit
> rm -rf dist && broccoli build dist
/Users/rlivsey/Development/ember-addons/mobiledoc/mobiledoc-kit/node_modules/broccoli-multi-builder/lib/utils/validate-node-modules.js:24
export function joinMobileDocs(doc1, doc2) {
const editor1 = new self.ContentKit.Editor({ mobiledoc: doc1 });
const editor2 = new self.ContentKit.Editor({ mobiledoc: doc2 });
editor1.render(document.createElement('div'));
editor2.render(document.createElement('div'));
const post1 = editor1.post;
const post2 = editor2.post;
@rlivsey
rlivsey / split-at-cursor.js
Last active October 23, 2015 13:28
Split editor at cursor and return serialized mobiledoc for each side
// Splits the current editor at the cursor into two mobiledocs
function splitAtCursor(editor) {
return editor.run(postEditor => {
const splitSections = postEditor.splitSection(editor.cursor.offsets.head);
const afterSection = splitSections[1];
let currentSection = afterSection;
let sectionsToMove = [afterSection];
while (currentSection.next) {
import Ember from 'ember';
export const KEY_CODES = {
backspace: 8,
tab: 9,
enter: 13,
escape: 27,
space: 32,
left: 37,
up: 38,
import ContentKitEditorComponent from 'ember-content-kit/components/content-kit-editor/component';
export default ContentKitEditorComponent.extend({
classNames: "custom",
setupEditor() {
const editor = this.get("editor");
editor.registerKeyCommand({
modifier: 1, // CMD
@rlivsey
rlivsey / application.controller.js
Last active October 1, 2015 09:41
Recursive Tree From List
import Ember from 'ember';
const TreeItem = Ember.Object.extend({
content: null,
children: null
});
export default Ember.Controller.extend({
data: [
{
import Ember from 'ember';
export default Ember.Controller.extend({
str: '',
isEmpty: Ember.computed("str", function() {
return Ember.isEmpty(this.get("str"));
})
});

Ember Scopes

Scoped component / services for teleporting data and actions through deeply nested components.

Often components can be deeply nested and something in the leaf can need data or send actions which the intermediate components shouldn't need to know about.

An example could be a gallery viewer, where any image can be clicked to launch a lightbox and needs to send an action up to the route to handle it.

Now if you add an image to a component which previously didn't have one, then you need to go and wire up every intermediate component to pass the action up. Even worse is if that gallery image component needs some data to act upon, now you're sending data up through layers which shouldn't need to care.

@rlivsey
rlivsey / controllers.application.js
Last active August 29, 2015 14:27
Computed not updating
import Ember from 'ember';
export default Ember.Controller.extend({
things: [1,2,3],
thingsAlias: Ember.computed.reads("things"),
thingsComputed: Ember.computed("thingsAlias.[]", {
get() {
// just copy it