Skip to content

Instantly share code, notes, and snippets.

Model Buffered Proxy Mixin

Makes it easy to work with models through a buffered proxy. Designed to work with the model relationship mixins and supports an identical API for buffered relationship management. This builds on the functionality of the Ember Buffered Proxy library.

New States

Normally a model has two states:

  • Clean: no unsaved changes
  • Dirty: some unsaved changes

A buffered model has four states:

  • Clean: no unsaved changes
import Ember from 'ember';
import SearchableSelect from 'ember-searchable-select/components/searchable-select';
import layout from './template';
const menuSelector = '.Searchable-select__options-list-scroll-wrapper';
export default SearchableSelect.extend({
layout,
classNames: ['Searchable-select-infinite'],
[
{
"id": 1,
"date": "2016-05-10T07:25:45",
"date_gmt": "2016-05-10T07:25:45",
"guid": {
"rendered": "https://demo.wp-api.org/?p=1"
},
"modified": "2016-05-10T07:25:45",
"modified_gmt": "2016-05-10T07:25:45",
{
"posts": {
"data": [
{
"id": 1,
"type": "posts",
"attributes": {
"title": "My title",
"content": "<p>Some markup in a string.</p>"
},
@heygrady
heygrady / jsonapi.json
Created January 5, 2017 20:08
Comparing generic WP-API response to JSONAPI
{
"data": {
"type": "page",
"id": "225",
"attributes": {
"slug": "about",
"title": "About",
"content": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In nec nunc molestie, pharetra tortor nec, tempus nisl. Sed mattis leo vitae imperdiet efficitur. Proin vitae nibh ante. Phasellus ut turpis vitae purus semper tincidunt. Donec sed elit neque. In in aliquam magna. Vivamus ex lorem, malesuada ut tincidunt id, tristique non est. Sed tempor lorem et sem finibus, ut tempor lacus mollis. Suspendisse ornare quis tellus ut convallis. Aliquam vestibulum ligula pulvinar, facilisis nibh lacinia, cursus est. Aenean eget rutrum metus. Suspendisse potenti. Nunc in condimentum lorem.</p>\n<p>Vivamus a laoreet augue. Etiam hendrerit metus nec sem tempus, a semper nisl dignissim. Duis tempor bibendum magna quis aliquet. Ut eget dui nec nisl feugiat eleifend. Nulla egestas volutpat nulla consectetur pharetra. Ut posuere, lorem sit amet volutpat interdum, ligula dolor tempus nisi, a t

Key concepts in a react-redux application

Redux is a collection of tools and techniques for managing a centralized "store". Essentially, your application state (your data) is kept in a central place (the store) and every part of the app that needs to read from that state does so in a controlled manner. At it's core, redux is mapping functional programming styles -- familiar to Lisp programmers -- to a modern JavaScript environment.

Many of the core concepts of react and redux can be found in other application frameworks like Angular and Ember. Classical (class based) frameworks like Ember provide a "kitchen sink" style API -- a prescriptive approach. Redux preaches a functional programming style, where composition and convention are preferred. This leads to a much smaller API but leaves a lot up to the developer.

This lack of a prescriptive API is freeing, but can lead to confusion when constructing your apps. It's up to the individual developer to follow best practices for separating concerns. The good

Using the classnames.bind method

Many people who work with React are familiar with the excellent classnames library. If you aren't familiar, it provides a simple function for gluing classnames together. In web programming in general, there are many times that we need to add or remove multiple classes based on conditional logic. The classnames library makes this easy.

More and more developers are embracing CSS Next and the power of CSS modules. However, when you add CSS modules to your react components, working with classnames gets more difficult. Typically, CSS modules is implemented with class name mangling. Transforming human readable class name strings into unique identifiers helps ensure that every class name in your app is unique.

This means that you can write your component CSS in isolation without worrying about the dreaded class name collisions that have plagued CSS

@heygrady
heygrady / mapDispatchToProps.md
Last active September 16, 2023 19:19
Redux containers: mapDispatchToProps

Redux containers: mapDispatchToProps

This document details some tips and tricks for creating redux containers. Specifically, this document is looking at the mapDispatchToProps argument of the connect function from [react-redux][react-redux]. There are many ways to write the same thing in redux. This gist covers the various forms that mapDispatchToProps can take.

@heygrady
heygrady / managing-url-state.md
Last active August 22, 2021 06:06
Managing location in react-router

Managing URL State

In a react-router (v4) app you will inevitably need to manage the URL. This is one of those easy-hard things that react-router provides a basic API for and leaves the rest of the implementation up to the user.

The sticking point is that react-router location.search and location.hash are stored as strings. Older versions of react-router would parse these values into a location.query object that is no longer available.

If you need to work with query strings as objects you will need a library like query-string. This will allow you to parse the location.search and location.hash strings as objects. You can then stringify query objects back into search strings and use them in a <Link /> or history.push() call.

What libraries are we using?

@heygrady
heygrady / using-react-router-as-a-store.md
Last active April 3, 2023 17:50
Using react-router as a store

Using react-router as a store

If you are building a react-redux application, you know that redux is the store. The state of your application should be kept in redux... but it's not the only store in your application. If you are using react-router (v4) you also have access to a "history store" — the URL of the current page hold valuable information about your application state.

We want to use react-router (v4) history to manage the state of the application too!

In a server-side react application, the URL is typically the only information available at render time. We populate the redux state using only the URL information we get from express. Treating the URL as a type of store has imortant implications in how you structure your app.

If you are truly dogmatic about only having one store to rule them all, you might enjoy react-router-redux. But it doesn't circumvent a key reality of a browser-based application: