Google analytics code

Friday, March 4, 2022

Angular: Can't bind to 'ngIfContext' since it isn't a known property of 'ng-container'

After a bit of refactoring our codebase this error started showing up during tests.

Can't bind to 'ngIfContext' since it isn't a known property of 'ng-container'

I found a few results from google but nothing that solved the problem. I started pulling out *ngIf from the tags in the template that was linked to the component and finally found the line causing the problem.

<ng-container *ngIf="group.subgroups; else singleGroup; context: {$implicit: group }">

*ngIf does not support the context argument. The context argument works on *ngTemplateOutlet property.

Thursday, May 6, 2021

RxJs / Angular: Changing a request parameter after a failure using retry

I'm making a request for a window of data based on a date and a range. The result of the request can be empty requiring me to make another request and change some parameters. The previous way I made the request was a recursive function that worked well but it can be a lot better.

Using a combination of rxjs pipe operators this can be one chain that works beautifully.

  

constructor(private http: HttpClient){}

someFunction() {
    const retryObject = { foo: 1};
    return of(retryObject)
        .pipe(
            switchMap(obj => {
        return this.http(`http://some.name?foo=${obj.foo}`);
    }),
    map(result => {
        if (result.error) {
            // Update the foo value and it will try the request again with the new value
            retryObject.foo++;
            throw 'an error happened';
        }
        // Happy path
    }),
    retry([HOW MANY TIMES YOU WANT TO TRY THE REQUEST])
).subscribe(result => {
        // Do something with your data
    })
}

The magic here is the retry pipe in combination with throwing an error. The linked page used throwError but it didn't retry the request. You can't pass in a string or number into the of operator. Javascript is pass-by-value for those. You need a reference to an object that can be updated.

Friday, October 2, 2015

Ionic Framework / UI Router: Remove a query parameter from the URI

I've been working with Ionic Framework for most of the year and finally launched the alpha version of my project. I've enjoyed working with it, but like everything you'll run into something that makes you bang your head on the desk for a few days. The most recent case of this was trying to remove a parameter from the URI.

If a user needs to reset their password I send them a URI that looks something like this.
http://this.that/login?resetKey=1234

After the user finishes the steps to reset I want to clean up the URI so if they hit the refresh button it doesn't trigger the reset process again.
http://this.that/login

I had the worse time finding an answer on google. After a few days of digging through UI Router documentation I finally found a combination that worked.

First you need to inject the $state param into your controller. We'll use the go method and redirect to the current URI using the current router name. The current route name can be accessed through the $state.current.name property. Next we need to specify the parameter we want to remove from the URI. We'll use an object with the key as the param name and the value will be null. Lastly we don't want to trigger the route again so we'll set the notify property to false.

The final command looks like this.
$state.go($state.current.name,{'resetKey': null},{'notify': false});

Such a simple command. So much digging. UGH!

Tuesday, June 23, 2015

HTML / CSS 3: Progress bar example

I've been working with the progress element and trying to get it to look nice across different browsers. It doesn't help that most of the styling elements are pseudo elements.

Here's the MDN page on the progress element.

The big issue I ran into was trying to style the element for Chrome and Firefox.
Chrome uses the following styles to change the look of the element:

progress::-webkit-progress-bar
This will style the background of the progress bar.

progress::-webkit-progress-value
This will style the growing value inside of the progress bar.

Firefox uses the following value:
progress::-moz-progress-value
This will only let you style the growing value inside of the progress bar. You can't style the background.

The other catch is you can't combine the values in your css. For example this won't work

progress::-moz-progress-value,
progress::-webkit-progress-value{

}

None of your styles will take and the bar will fall back to it's unstyled default.

Here's the html I was playing with.
Here's a link to a jsFiddle of the following code


/* http://www.hongkiat.com/blog/html5-progress-bar/ */
.progress-bar{
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
}

/* Can't share styles. Chome gets angry */
/* FF style the progress bar */
.progress-bar::-moz-progress-value{
    background: #3399ff;
    border-radius: 12px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.25) inset;
}

/* Chrome style the background of the progress bar */
.progress-bar::-webkit-progress-bar{
    background: silver;
    border-radius: 12px;
}

/* Chrome style the progress value */
.progress-bar::-webkit-progress-value{
    background: #3399ff;
    border-radius: 12px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.25) inset;
}

Wednesday, September 17, 2014

Sencha Touch: Android Halo Light Theme

One of the things I love about Sencha Touch is the built in themes. Android has 4.x has two themes: Halo Dark, Halo Light.  There is only one Android theme for Sencha Touch and it's dark. I've been bugging them for awhile to include a light theme and their response was "You do it".

I googled for a long time and couldn't find a theme someone else did. I gave in and decided to make my own theme after finding this one. It's beautiful. I read his breakdown and he created a theme by copying the winPho theme and made changes until he was happy. I did something similar. I copied the Android theme and started digging in.

That's when I found it. There is a Sass variable in /[Sencha SDK folder]/resources/themes/stylesheets/sencha-touch/mountainview/var/_Class.scss called $dark-theme. If you set this variable to false in your local sass file for android it becomes a light theme.

I wish this was explained better. It's only through source code diving can we find the true answers.

Zend Framework 2: Using mysql functions in SQL classes

The built in ZF2 classes for sql queries have been wonderful to use. I like that it takes care of escaping data and saves you from messy string all over your codebase. I ran into a problem when trying to do something "complex".

Here is an example of the SQL I was trying to write.
INSERT into `table` SET `col1` = FROM_UNIXTIME(?)

When I tried running this the mysql method was quoted
`FROM_UNIXTIME(?)`
I started digging around and found an extra class required if you want to use functions in the escaped data spot. It's called an expression. Here is how they're used in the insert class.

$insert->into($tableName)
       ->values([`col` => new \Zend\Db\Sql\Expression("FROM_UNIXTIME(?)","?", [\Zend\Db\Sql\Expression::TYPE_VALUE])]);

Now the function won't be quoted and the value can be safely escaped when execute is run.


Here's some documentation on how expressions are used in a where clause.

Monday, July 28, 2014

Angular: Form element not attaching to $scope

I'm well into a redesign using AngularJS as my front-end framework. After a small learning period I'm in love. It's a fantastic way to tie your UI to your backend. That said there is still some work to be done.

I'm breaking my project into modules that are included with the ng-include tag. This helps shrink the code on the page into manageable chunks. It nice when you're using a very verbose framework like Bootstrap.

All of my forms are wired up using Angular's form validation. Once again this is fantastic. Being able to watch the inputs in real time and display helpful errors is great. I ran into an issue trying to reuse a form after I've removed it from the users view.

<form name="createUser">
    <input name="name" ng-model="formObj.name" />
    <input name="email" ng-model="formObj.email" />
</form>

I should be able to access the form in my angular in this fashion.

// This will reset any validation errors on the form
$scope.createUser.$setPristine();

This wasn't working for me and I spent a lot of time trying to figure out why. Here is how my code looked.

<div ng-controller=“controllerName”>
    <div ng-include=“ ‘path-to-the-template’ ”></div>
</div>

<!—- Inside path-to-the-template -—>
<form name="createUser">
    <input name="name" ng-model="formObj.name" />
    <input name="email" ng-model="formObj.email" />
</form>

One thing to consider is when Angular is starting up the controller code the form isn't attached yet. When you click the button that submits the form it will be attached by that time. I was still unable to access the form element.

Update

I submitted this as a bug to the git repo and got a response very quickly. That team is really on top of things. Turns out when you use ng-include it creates a new scope var. This is how the setup should look.

<!—- The vars should live in the controller. I placed them here for the example. -—>
<div ng-controller=“controllerName” ng-init="form={}; model={}" >
    <div ng-include=“ ‘path-to-the-template’ ”></div>
</div>

<!—- Inside path-to-the-template -—>
<form name="form.createUser">
    <input name="name" ng-model="model.name" />
    <input name="email" ng-model="model.email" />
</form>

Now the include can reference the variables from the parent scope. The form and it's input should now be accessible.

Friday, June 27, 2014

Zend Framework 2.x Learn ZF2 by Example: Protecting our Pages - Authentication type

I'm starting to implement authentication on my web app and I ran into a problem with the example in the book.

The example on page 146 uses MD5 as a encryption scheme.

$adapter = new DbAdapter($db,
                         'users',
                         'login',
                         'password',
                         'MD5(password)');

The adapter builds a simple SQL query that will check the table, match the user name and password then let you know if it's valid. The problem lies in the 5th parameter. When I tried to match an account I know works it kept saying the account wasn't valid. After digging into the documentation I found the 5th parameter wasn't configured right. It should be 'MD5(?)'.

$adapter = new DbAdapter($db,
                         'users',
                         'login',
                         'password',
                         'MD5(?)');

After the change the auth adapter works perfectly. Hope this helps save you some time.

Thursday, June 5, 2014

Sencha Touch Themes: Using built-in themes for iOS and Android

One of the things I like about Sencha Touch is the built in device themes. I like being able to make a web app look native.

I found many resources that talked about the themes but none gave very good instructions on how to make them work. I finally found a video that walked you through some of the process involved in making themes work. It left out some important stuff that I found through lots of trial and even more error.

I'll show you how to apply a theme to the default sencha project. You can apply these steps to your own project.

  1. Create an example app.
    • sencha -sdk [path to sdk] generate app [AppName] [path]
  2. You'll need to create scss files for the different platforms you want to support.
    • cupertino.scss (iOS 7 styling)
    • cupertino-classic.scss (iOS 6 styling) 
    • mountainview.scss (Android)
  3. Each of these files will include two imports lines that define our theme styles.

    @import 'sencha-touch/[platform name]';
    @import 'sencha-touch/[platform name]/all';

    [platform name] is the name of the style you're including. cupertino.scss will use "cupertino" and mountainview.scss will use "mountainview". All the platform names are available here under tip 2.
  4. Next you'll need to run "compass compile" or "compass watch" in the resources/sass folder. 
  5. Now we have to update app.json to include the new css files that were generated by compass. Look for the css option and add each platform as an object to the css array.
    {
        "path": "resources/css/cupertino.css",
        "platform": ["ios"],
        "update": "delta"
    }

    Make sure to update the platform value with the appropriate value. The options are available here under available platforms.

    IMPORTANT NOTE: Make sure to add the "platform": ['desktop'] to the app.css entry. If you don't app.css will be included in every theme and it will break the display. 

  6. Lastly you'll need to run sencha app build to bring everything together. You can test the different templates by appending ?platform=[platform name] to your uri.
    eg: senchaapp.io/?platform=ios
I've added the example project to github so you can pull that down as an example.

Tuesday, April 22, 2014

Zend Framework 2 & MAMP : Memory Issues

I ran into a problem working through the tutorial for Learn ZF2 for form validation. I reached the section where the form data would be inserted into a database and that wasn't possible using the command line PHP server. I decided to switch to MAMP because it has more stuff baked in.

I quickly ran into an issue that was tough to track down. After added the database code the form no longer submitted. After ripping out lots of code and not finding an answer I called it a night. The next day I figured out the problem was with MAMP not my code. If I disabled the file validation on the form everything worked fine.

This was the error I ran into
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 32 bytes) in /Volumes/zend_framework/learnzf2/vendor/zendframework/zendframework/library/Zend/Stdlib/ErrorHandler.php on line 113

Not the most straight forward bug. Turns out the issue is the command line PHP server has a larger memory_limit size. It's 128M while MAMP is set to 32M. It overflowed after processing the file. After upping memory_limit to 128M and restarting the server everything worked fine.

The memory_limit var is stored in php.ini. It can be reached by using
File => Edit template => php => php 5.4
Find memory_limit and change it to 128M.