Call jQuery Functions from Dart

The standard documentation for Dart and JavaScript interoperability isn’t great. It refers to the ‘dart:js’ library which although works is a bit verbose to get things done.

For instance, to call a jQuery function, you would need

import 'dart:js';
 
...
context.callMethod(r'$', ['.some.class.selector']).callMethod('someFunction');
...

The dart team has since then released the js package with which you can simply call

import 'package:js/js.dart';
 
...
context.$('.some.class.selector').someFunction();
...

Basically native syntax. Of course you have to add it to the yaml and pub get again.

Moving Between Directory History in Fish Shell

Fish shell is supreme! Finally a shell whose config doesn’t look like a Perl hack. Too bad it doesn’t get much publicity.

Here’s an awesome feature. In zsh, I’d be used to doing stuff like

dirs -v
cd -2

to move between previous directories. In fish, there’s a cd – to move to the previous history but much more powerfully, you can just do alt-left/right arrow to move through your entire directory history without typing anything.

It’s boss!

Functional Programming Example in Dart

For a quick reference.

At the time of writing (Feb 2014), the top results from a Google search on ‘Dart functional programming’ or ‘Dart list comprehension’ don’t immediately yield clear examples. That’s crazy given that Dart is such a boss language. So here’s an example:

// double all values
List myList = [1, 2, 3, 4];
List doubledList = myList.map((x) => x * 2);

You can do the standard map, reduce, filter on Iterables on Dart. Of course you can chain them up like other languages. Although it’s not strictly using the set building notation like in Python, you can still pretty much do everything so something like:

[x * 2 for x in myList if x % 2 == 0]

in Python would look like:

myList.where((x) => x % 2 == 0).map((x) => x * 2);

Iterating Python Lists 2 by 2

Not necessarily the most superior way performance/Pythonicity wise but it’s simple to remember and short:

list = [1, 2, 3, 4, 5, 6, ...]
pairs = zip(list[::2], list[1::2])
for pair in pairs:
    # do stuff

pair will be (1, 2), (3, 4) etc.
The 2 list slices turn the original into odd/even sets and zip merges them

Alternative to HttpClient for Windows Phone 7.5

If you wanted to move your WinRT project to support Windows Phone 7.5, you’ll rejoice at the recent news that Microsoft’s Microsoft.bcl.Async package in Nuget that allows using Async keywords and Task class but in the end, you really used them in WinRT because HttpClient class had a GetAsync method which is not available outside WinRT.

Worry not, the Microsoft.bcl.Async package comes with a Microsoft.Threading.Tasks.Extensions assembly that if you look inside, includes:

public static System.Threading.Tasks.Task<webresponse>GetResponseAsync(this System.Net.WebRequest source)
Member of AsyncExtensions</webresponse>

Which means you can use System.Net.WebRequest in its place and it supports Task/Async.

So you can do something like:

WebRequest request = WebRequest.Create(uri);
await request.GetResponseAsync();

How to Debug/Deploy WinRT/Windows 8 App to ARM Tablet

You’ve now made your app and tested it on desktop and want to use the same code for your developer unlocked the tablet. Until there’s an ARM version of Visual Studio, here’s what you can do. The basic process would be to set up a Remote Debugger service on your tablet and from a desktop on the same subnet, run on it directly. This assumes you have a development tablet device and a desktop running the same user (who also has admin rights) Read More…

Sharing to Facebook/Twitter from Windows 8/WinRT App

Sharing on “Metro style”/”Windows Store” app is a bit different from what we used to. Instead of invoking it directly in code, Microsoft encourages us to use the centralised “charm” bar where we would then implement a page event handler to fill in details to share.

To personalise share details, we would fill a DataPackage that would then be used across various media such as email or message or other apps. Read More…

Django ModelForm Required ForeignKey not Connecting?

You have a ModelForm based on a Model with a non-optional ForeignKey field. You already connected it via something like

instance = SomeClass(someRelatedObject = someObject)
form = SomeForm(request.POST, instance = instance)

but form.is_valid() is still giving you an error saying someRelatedObject can’t be null. One possible solution is to check your SomeClass’s definition of someRelatedObject. Try putting ‘editable = False’ in your ForeignKey constructor and it may solve your problem.

No Chinese Fonts on XBMC Eden 11.0

Simple solution. Many posts online points to replacing fonts etc but with Eden, the included Arial has unicode support. You just had a shitty theme with custom non-unicode font. Just go to the skinning section of your theme (skins can change the hierarchy) and change the font from default to Arial. Easy!

Solve XBMC Tearing on Ubuntu 12.04

Definitely not an exhaustive fix but did fix it for me on a Zotac ID80 with Nvidia GT 520m. Disable composite with

sudo nvidia-xconfig --no-composite

It’s a very obscure note on the XBMC’s unreachable wiki. It mentions that XBMC is not compatible with Compiz and has a link to a german site that among other things, has the code above. It works.

Django Development Server with HTTPS

Sometimes you can’t avoid using HTTPS right starting from development (such as for developing Facebook canvas applications). You can setup Apache on your local machine and go through a bunch of trouble setting up your domain and setting up the certificate etc but there’s an easy way of using Django’s python manage.py runserver to work with HTTPS.

This can be done with stunnel that lets the Facebook server and stunnel on your machine communicate in SSL and stunnel turns around to communicate with Python in HTTP. First install stunnel. For instance in Mac OS X:

brew install stunnel

Read More…

How to Change Disabled Button Background, Focused TextBox Border etc in Windows Phone

In the properties tab or attributes of XAML, the level of control you can have over Silverlight controls is rather limited. You can’t change their behaviours in different states. To actually control it, you need to create a ControlTemplate. Sounds innocent enough but it’s basically forcing the user to redefine a user control and recreate the button or the textbox manually from their constituents (border, grid etc) which is extremely labour intensive if all you want to do is just change say the colour of the border when it is selected.

Instead of recreating the control from scratch, you can let Microsoft Expression Blend do some part of that job for you. Style your control to your liking as much as you can in Blend and then right-click to select Edit Template -> Edit a Copy. Then you get to define a key. Think of it like a CSS class and name it as you wish. Define it in the Application (App.xaml) so you can use it everywhere in your application. This will then generate a ControlTemplate for you based on the instance of control you selected Edit Template on. Read More…

1 2 3 6  Scroll to top