Skip to content

Instantly share code, notes, and snippets.

View kalyco's full-sized avatar

Kayla Comalli kalyco

  • 6 River Systems
  • Boston, MA
View GitHub Profile
@kalyco
kalyco / deaf_grandma
Created November 20, 2014 23:33
Deaf Grandma
puts 'SONNY..? IS THAT YOU..?'
input1 = ''
bye_count = 0
while bye_count < 3
input1 = gets.chomp
if input1 == 'BYE'
bye_count += 1
puts 'NO. NOT SINCE '+(rand(1920..1956)).to_s
elsif input1.upcase == input1
puts puts 'NO. NOT SINCE '+(rand(1920..1956)).to_s
@kalyco
kalyco / dijkstra.rb
Last active August 29, 2015 14:20 — forked from yaraki/dijkstra.rb
#!/usr/bin/ruby1.9.1 -Kw
# -*- coding: utf-8 -*-
class Edge
attr_accessor :src, :dst, :length
def initialize(src, dst, length = 1)
@src = src
@dst = dst
@length = length
@kalyco
kalyco / ember model hook
Created June 9, 2015 14:03
In ember first create the route object, then use the model hook to get the data you need from it.
App.UserRoute = Ember.Route.extend({
model: function(params) {
return this.store.find(user, params.user_id);
}
)};
@kalyco
kalyco / ember controller
Created June 9, 2015 15:26
ember controllers are different from rails controllers in that their primary function is to provide the logic to access data from the model. In this example, the user has different first_name, last_name properties, but wants to show a full name. This is something that a controller would be used for.
App.UserController = Ember.Object.Controller.extend({
fullName: function() {
return this.get("first_name") + " " + this.get("last_name");
}
})
@kalyco
kalyco / ember view
Created June 9, 2015 16:05
View main function: 1. Creating reusable components. 2. Setting up logic to handle events
App.ProfilePhotoView = Ember.View.extend({
click: function(evt) {
this.get('controller').send('ExpandProfilePhoto');
}
});
App.UserController = Ember.ObjectController.extend({
actions: {
expandProfilePhoto: function() {
//get the expanded picture and display it
@kalyco
kalyco / ember event response
Last active August 29, 2015 14:22
Binds the field to a property in the controller and lets Ember handle keeping things updated. In this example a controller is set up with 2 properties 'number' and 'timesTwo'
// in the controller
App.NumberController = Ember.ObjectController.extend({
number: 1,
timesTwo: function() {
return Number(this.get('number')) * 2;
}.property('number') //tells it to treat it as a dynamic property that updates whenever number updates.
});
//in the template
{{input type="text" value=number size=50}} x2 = {{timesTwo}}
@kalyco
kalyco / ember order of operations
Created June 9, 2015 17:55
Ember's order of operations
1. User hits a URL
2. Ember's Router recognizes it and passes it to a Route object
3. The Route sets up a Controller and supplies it with a Model
4. The Model provides the View layer (represented by a Template) access to the data
5. Any properties defined by the Model are available to the Controller
ex: if User has first_name, your template has direct access to it through the Controller
*** don't need to define an empty controller method like in Rails ***
*** Ember will also generate the controller for you if one does not exist ***
@kalyco
kalyco / ember observe vs property method
Last active August 29, 2015 14:22
Data binding work is done by observers, that watch for changes, then fire off events (or updates data) when something happens. This change can be responded to in one of two ways.
// 1: Property method
App.NumberController = Ember.ObjectController.extend({
number: 1,
timesTwo: function() {
return Number(this.get('number')) * 2;
}.property('number')
});
// "property" lets you use the return value of a function as if it was a static value.
// if you add "property" to the end of your function and you can bind it to
// your template and display the value there.
@kalyco
kalyco / rails JSON serializer for ember
Created June 9, 2015 19:24
Formatting rails data to a JSON through a serializer
# a rails controller set up to send user information to ember's profile page
class UsersController < Devise::SessionsController
def show
@user = User.find(params[:user_id])
respond_to do |format|
format.json { render json: @user }
end
end
end
//For simple applications,, Ember Data's REST adapter can handle all
//asynchronous calls to the Rails server and completely insulate the
//dev from worrying about what happens when the server takes longer
//to respond than expected.
//If ever required to make a call to the server outside of a Route object's
//Model hook, you'll need to ensure that your application responds properly
//to when the call either fails or succeeds, and not to do anything about the
//call until it returns.