Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / controllers.js
Last active August 29, 2015 14:02
AngularJS: Version with global variables.
app.controller('mainCtrl', function($rootScope, UserService) {
$rootScope.users = [];
$rootScope.todos = [];
// Get all users.
UserService.all().then(function(users) {
angular.forEach(users, function(user) {
$scope.users.push(user);
// Get all todos.
@mrded
mrded / backend.module
Created June 12, 2014 10:56
Drupal 7: Show ctools modal via JS.
<?php
drupal_add_js('misc/ajax.js');
ctools_add_js('modal');
ctools_add_css('modal');
// Add your modal style to the settings. You find the defaults in modal.js near the top:
drupal_add_js(array(
'my-modal-style' => array(
'modalSize' => array(
@mrded
mrded / module.php
Created June 16, 2014 10:07
Drupal 7: Entity metadata wrappers
<?php
//@see: https://drupal.org/node/1021556
$wrapper = entity_metadata_wrapper('node', $node);
$wrapper->author->mail->value();
$wrapper->author->mail->set('[email protected]');
$wrapper->author->mail = '[email protected]';
// Unified way of getting $node->title, $user->name, ...
@mrded
mrded / foo.module
Created June 25, 2014 16:48
Drupal 7: FQL get user's current_location
<?php
$access_token = 'woknfowenf';
$query = 'SELECT blabla... FROM blabla...';
$result = fboauth_graph_query('fql', $access_token, array('q' => $query));