Skip to content

Instantly share code, notes, and snippets.

@OzieWest
OzieWest / dynamic-select.cs
Last active August 29, 2015 14:02
Snippets
public Func<T,T> AsProjections<T>(string fields) where T: class
{
var xParameter = Expression.Parameter(typeof(T), "o");
var xNew = Expression.New(typeof(T));
var bindings = fields.Split('.')
.Select(o => o.Trim())
.Select(o =>
{
@OzieWest
OzieWest / js-links.html
Last active August 29, 2015 14:02
JavaScript General
$rootScope.$broadcast('scanner-started', { any: {} });
$scope.$on('scanner-started', function(event, args) {
var anyThing = args.any;
});
@OzieWest
OzieWest / snippets.js
Created June 18, 2014 12:16
RequireJS
// Файл myModule.js - определяем
define(function() {
var app = {
val: 1
};
return app;
});
// в любом другом файле
@OzieWest
OzieWest / dynamically-adding-js-to-asp.cs
Created June 23, 2014 06:26
Dynamically adding js to ASP.NET
var js = new HtmlGenericControl("script");
js.Attributes["type"] = "text/javascript";
js.Attributes["src"] = "myFile.js";
Page.Header.Controls.Add(js);
@OzieWest
OzieWest / nHibernate-to-json.cs
Created June 23, 2014 06:29
JSON.NET and nHibernate Lazy Loading of Collections
public class NHibernateContractResolver : DefaultContractResolver {
protected override List<MemberInfo> GetSerializableMembers(Type objectType) {
if (typeof(INHibernateProxy).IsAssignableFrom(objectType)) {
return base.GetSerializableMembers(objectType.BaseType);
} else {
return base.GetSerializableMembers(objectType);
}
}
}
@OzieWest
OzieWest / directive.js
Created June 24, 2014 19:03
Invoke func after ngRepeat
app.directive("repeatComplete", function( $rootScope ) {
// уникальный ID (ведь может быть несколько вложенных ng-repeat)
var uuid = 0;
// компилируем DOM узел до того как он будет залинкован директивой ng-repeat
function compile( tElement, tAttributes ) {
// получаем уникальный ID
var id = ++uuid;
@OzieWest
OzieWest / directive.number.js
Created June 25, 2014 11:10
Только цифры в input
angular.module('myApp', []).directive('numbersOnly', function(){
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function (inputValue) {
if (inputValue == undefined) return ''
var transformedInput = inputValue.replace(/[^0-9]/g, '');
if (transformedInput!=inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
@OzieWest
OzieWest / debugging-angular-js-apps-from-the-console.js
Created July 1, 2014 16:11
Debugging AngularJS Apps from the Console
1: Access Scopes
angular.element(targetNode).scope()
// ChildScope {$id: "005", this: ChildScope, $$listeners: Object, $$listenerCount: Object, $parent: Scope…}
angular.element(targetNode).isolateScope()
// Scope {$id: "009", $$childTail: ChildScope, $$childHead: ChildScope, $$prevSibling: ChildScope, $$nextSibling: Scope…}
2: Grab any Services
angular.element('html').injector().get('MyService')
// Object {undo: function, redo: function, _pushAction: function, newDocument: function, init: function…}
@OzieWest
OzieWest / NotNull.cs
Created July 2, 2014 11:40
On accessing chains of potentially null properties
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
namespace Helpers {
public static class NotNull {
public static TProp Get<TSource, TProp>(this TSource source, Expression<Func<TSource, TProp>> property) where TSource : class {
if (source == null) return default(TProp);
var current = property.Body;