Skip to content

Instantly share code, notes, and snippets.

View jasonporritt's full-sized avatar

Jason Porritt jasonporritt

View GitHub Profile
@jasonporritt
jasonporritt / offline-mode-route-helpers.coffee
Last active November 18, 2015 17:37
Load an Ember Data hasMany relationship after possible network request failures
OfflineModeRouteHelpers = Em.Mixin.create
robustlyLoadHasMany: (model, relationshipName) ->
new Em.RSVP.Promise (resolve, reject) =>
model.get(relationshipName).then resolve, (error) =>
internalModels = model.get("#{relationshipName}.content.canonicalState")
internalModels.forEach (internalModel) ->
internalModel._loadingPromise = null
internalModel.transitionTo('empty')
model.get(relationshipName).reload().then resolve, reject
/**
* `Ember.MergedArray` is an array that observes multiple other arrays (called source arrays) for changes and includes
* all items from all source arrays in an efficient way.
*
* Usage:
*
* ```javascript
* var obj = Ember.Object.create({
* people: [
* {
App.DynamicBoundTextField = Ember.TextField.extend
placeholderBinding: 'content.name'
# Update the remote property's value when the value of
# our text field changes
_setRemoteValue:(->
val = @get('value')
@set("controller.data.#{@get('content.key')}", val) if val?
).observes('value')
{"fields": [
{ "name": "First Name", "key": "first_name" },
{ "name": "Last Name", "key": "last_name" },
{ "name": "Email", "key": "email" }
]}
@jasonporritt
jasonporritt / motion_stump_test_snippet.rb
Created December 3, 2012 00:57
More complete motion-stump test snippet
before do
@target = ObjectUnderTest.new
end
it 'posts a notification named "Jump" with the height' do
@target.notifCenter = mock('postNotificationName:object:userInfo:') do
| name, object, userInfo |
name.should == 'Jump'
object.should == @target
userInfo.should == { 'height' => '10m' }
end
@jasonporritt
jasonporritt / motion_stump_doc_snippet.rb
Created December 3, 2012 00:51
motion-stump-doc-snippet
# Pure stub objects
my_stub = stub(:thing, :return => "dude, a thing!")
my_stub.thing # => "dude, a thing!"
# Mocking right on the object
my_object = "things are fun"
my_object.mock!(:fancy, :return => "ooo fancy!")
my_object.fancy # => "ooo fancy!"
@jasonporritt
jasonporritt / rubymotion_count_array_vs_nsarray.rb
Created November 15, 2012 21:28
RubyMotion count method with NSArray vs. Array
describe Array do
it "calls the Rubyish count method on Rubyish arrays" do
count = [1,2,3,4,5,6].count { |i| i > 3 }
count.should == 3
end
it "doesn't call the Rubyish count method on NSArray objects" do
nsarray = NSArray.arrayWithArray([1,2,3,4,5,6])
nsarrayCount = nsarray.count { |i| i > 3 }
# The block above gets ignored and a warning is printed out in the console, so...
@jasonporritt
jasonporritt / memoization_declaration_as_property.cs
Created October 27, 2011 14:06
Memoization in C#: Declaration as Property
public Func<string, string> WithOneInput = Memoizer.Memoize(
(string x) =>
{
return String.Format("Hello, {0}", x);
}
);
@jasonporritt
jasonporritt / memoization_complex_input.cs
Created October 27, 2011 13:59
Memoization in C#: Complex Input
public static Func<TSource1, TSource2, TReturn> Memoize<TSource1, TSource2, TReturn>(Func<TSource1, TSource2, TReturn> func)
{
var cache = new Dictionary<string, TReturn>();
return (s1, s2) =>
{
var key = s1.GetHashCode().ToString() + s2.GetHashCode().ToString();
if (!cache.ContainsKey(key))
{
cache[key] = func(s1, s2);
}
@jasonporritt
jasonporritt / memoization_one_input.cs
Created October 27, 2011 13:58
Memoization in C#: One Input
public static Func<TSource, TReturn> Memoize<TSource, TReturn>(Func<TSource, TReturn> func)
{
var cache = new Dictionary<TSource, TReturn>();
return s =>
{
if (!cache.ContainsKey(s))
{
cache[s] = func(s);
}
return cache[s];