Skip to content

Instantly share code, notes, and snippets.

@mrded
mrded / FindAndUpdate.js
Last active August 29, 2015 14:02
AngularJS: Find and update object
app.controller('appCtrl', function($scope, $filter) {
//..
angular.forEach($scope.todos, function(todo) {
if (todo.id == updatedTodo.id) {
angular.extend(todo, updatedTodo);
}
});
});
@mrded
mrded / GlobalCtrl.js
Last active August 29, 2015 14:01
AngularJS: Example of filtering problem.
.controller('GlobalCtrl', function($rootScope, $scope, UserService, TodoService) {
$rootScope.users = [];
$rootScope.todos = [];
// Init.
UserService.all().then(function(users) {
angular.forEach(users, function(user) {
rootScope.users.push(user);
TodoService.all(user.id).then(function(todos) {
@mrded
mrded / array.js
Created May 10, 2014 21:17
AngularJS: Delete an item or object from an array
var array = [];
var index = $scope.array.indexOf(item);
$scope.array.splice(index, 1);
@mrded
mrded / csrf.js
Last active April 24, 2024 13:17
AngularJS: add csrf token to angular http requests
angular.config(function($httpProvider) {
var authToken = $('meta[name="csrf-token"]').attr('content');
$httpProvider.defaults.headers.common["X-CSRF-TOKEN"] = authToken;
});
@mrded
mrded / example.rb
Last active August 29, 2015 14:01
Rails: array of IDs
# If you want to go directly to the DB
ActiveRecord::Base.connection.select_values("select id from users")
# If you already have a model
User.all(:select => :id).collect(&:id)
# In Rails 3 you can do this
User.pluck(:id)
# Rails helper
@mrded
mrded / index.haml
Last active August 29, 2015 14:01
AngularJS: Ng-Repeat array to rows and columns
.row
.col-lg-4
%div{'ng:repeat' => "item in array.slice(0, array.length / 3)"}
{{item}}
.col-lg-4
%div{'ng:repeat' => "item in array.slice(array.length / 3, array.length * 2/3)"}
{{item}}
.col-lg-4
%div{'ng:repeat' => "item in array.slice(array.length * 2/3, array.length"}
{{item}}
@mrded
mrded / index.html
Created May 7, 2014 18:14
AngularJS: First item as active class
<ul class="nav nav-tabs">
<li ng-repeat="team in teams" ng-class="{active: $index == 0}">
<a href="#" data-toggle="tab">{{team.name}}</a>
</li>
</ul>
@mrded
mrded / example.module
Last active August 29, 2015 14:00
Drupal7: Update form element via AJAX.
<?php
function example_form($form, &$form_state) {
$form['foo'] = array(
'#type' => 'select',
'#title' => t('Foo'),
'#options' => array('one', 'two', 'three'),
'#ajax' => array(
'callback' => 'example_foo_callback',
'wrapper' => 'bar-sections'
@mrded
mrded / array_object_bug.php
Last active August 29, 2015 13:59
PHP: ArrayObject bug
<?php
$obj = new ArrayObject();
$obj['hello'] = NULL;
print 'ArrayObject<br >';
var_dump(empty($obj['hello'])); // TRUE
var_dump(isset($obj['hello'])); // FALSE
var_dump(array_key_exists('hello', $obj)); // TRUE
class Test1 extends ArrayObject {
@mrded
mrded / install.sh
Created March 8, 2014 15:09
Sample Mobile Application (Angular.js, Cordova, Ionic, PhoneGap)
npm install -g cordova
npm install -g ionic
ionic start myApp
cd myApp
ionic platform ios
ionic emulate ios
ionic run ios