Saturday, March 7, 2015

Angular ng-repeat scope

Need to know that angular ng-repeat directive actually create child scope for each element.
so if you want reference the parent $scope value need to use $parent

<span ng-show="item.name == currentDbName"><input type="text" name="newDbValue" ng-model="$parent.newDbValue" />{{$parent.newDbValue}}</span>

Or else if you want to update the repeating item value, you need to use $index

<div ng-repeat="name in dbnames track by $index">
        <input type="text" ng-model="dbnames[$index]" />
</div>

the "track by $index" is new since Angular1.2 to make sure the text box won't lose focus for each key stroke

Tuesday, February 3, 2015

Angular test primer in Jasmine

To test angular controller in Jasmine, need to use the angular mocks to inject module and controller
Then in the controllerSpec test js file, we can inject the controller and scope

describe('expense controller', function () {

var $controllerTestConstructor;
var scope;

beforeEach(module('app'));

beforeEach(inject(function($controller, $rootScope){



$controllerTestConstructor = $controller;

scope = $rootScope;

}));
 
it('should have three expense item', function () {
var ctrl = $controllerTestConstructor('expenseController', { $scope: scope });

expect(ctrl.expenseItems.length).toBe(3);

});

});
 

Angular module dependency injection

Angular provides the module functions to do the service injection. In this way, we can build our own service functions and inject into the controller to load it.

We can set up the module first, then call the app.js to set up the both module together, this is the example shows in the angular tutorial site.
https://docs.angularjs.org/tutorial/step_11

Here is the example for this way, there is a controller and it depends on the dataService to load the data. Each angular.module call is in its own js file.

angular.module('app', ['services', 'controllers']);

angular.module('services', [])
        .factory('expenseDataService', ['$http', expenseDataService]);

angular.module('controllers', [])
        .controller('expenseController', ['expenseDataService', expenseController]);

Also there is a simplified version. But you need to put app.js at first in the html page. In this way, all the dependency are created in the app module directly.
angular.module('app', []);

angular.module('app')
        .factory('expenseDataService', ['$http', expenseDataService]);

angular.module('app')
        .controller('expenseController', ['expenseDataService', expenseController]);

The full file is attached as a zip file.expense_app.zip

Sunday, February 1, 2015

How to install / use bower in Visual studio 2013 with proxy settings

Bower is very popular to manage client script package, here is the steps to set it up to use it in visual studio 2013.

1. Open package management console and search bower (not bower.js), install it.
    After it is installed, you should be able to open a command prompt and go to your visual studio project folder, then run "bower install <package>" from there.

2. By default, Bower installed the package into a folder called bower_components under the project folder. Usually we want to manage the packages better and put them under scripts/lib folder.

To do this, create a file named the .bowerrc and add the line,
{

"Directory":"Scripts/lib",
 
}

This will install all the packages into the scripts/lib folder. By default, windows doesn't allow you create a file starts with period (.), then you can create any temp text file and rename it in the command window.

3. If the visual studio runs internally behind a firewall, you need to set up the proxy for both Bower and Node.
   
    In the .bowerrc file, add the line,
"https-proxy": "http://<domain>%5C<user>:<passsword>@<proxy_server>:<proxy_port>/"
    Edit the node.cmd under .bin folder to add the lines,
SET HTTP_PROXY=http://1:1@192.168.11.22:8080
SET HTTPS_PROXY=http://1:1@192.168.11.22:8080

Thursday, January 22, 2015

In MVC5, How to merge the IdentityDbContext into custom db context

Here is the code as an example,

1. Add the custom user login and user role configurations

    public class IdentityUserLoginConfiguration : EntityTypeConfiguration<IdentityUserLogin>
    {
        public IdentityUserLoginConfiguration()
        {
            HasKey(iul => iul.UserId);
        }
    }
    public class IdentityUserRoleConfiguration : EntityTypeConfiguration<IdentityUserRole>
    {
        public IdentityUserRoleConfiguration()
        {
            HasKey(iur => iur.RoleId);
        }
    }

2. Inherit your custom dbcontext from IdentityDbContext,

    public class CustomDbContext : IdentityDbContext<ApplicationUser>
    { public CustomDbContext() : base("DefaultConnection")
        {
        }
        public DbSet<CustomEntity> Entities { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Configurations.Add(new IdentityUserLoginConfiguration());
            modelBuilder.Configurations.Add(new IdentityUserRoleConfiguration());
        }
    }

Wednesday, September 10, 2014

SharePoint TroubleShooting - Can not access local farm when running script in sharepoint powershell

When you run Get-SPSite or Get-SPWeb in a SharePoint powershell window and get the following error,

Can not access local farm...

It is usually the permission issue. The user execute the power shell cmd is not in the SharePoint_Shell_Access role, just run this command, Add-SPShellAdmin -UserName ... to add the user will be fine,

Here is the reference links,
http://technet.microsoft.com/en-us/library/ee806878(v=office.14).aspx#section2
http://technet.microsoft.com/en-us/library/ff607596(v=office.14).aspx

Monday, August 25, 2014

SharePoint trouble shooting - update list data without event receiver triggered in powershell

Sometimes we need to run powershell to do batch update some list items, if the event receiver is active, the update process can be very slow. In this case, we will want to turn it off before batch update, then turn it back on.

Here is the powershell script to complete this,

# disable event firing
$my = [Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint");
$type = $my.GetType("Microsoft.SharePoint.SPEventManager");
$prop = $type.GetProperty([string]"EventFiringDisabled",[System.Reflection.BindingFlags] ([System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Static));
$prop.SetValue($null, $true, $null);
**** run times update ****

# enable event firing
$prop.SetValue($null, $false, $null);