Page 3 of 4

Q&A: sails.js enable jsonp

Question

This is a question I had myself. Why, if I call /foo?callback=bar do I not get a jsonp response?

Answer

Out of the box, sails doesn’t enable jsonp support. In order to enable this, you have to change config/blueprints.js and add jsonp: true.

Q&A: Sails.js won’t run through forever.js

Question

Since recent releases of sails.js, for some people it seems like sails won’t run using forever anymore.

On github, SuThaw asked:

I installed forever js with “npm install forever -g” and I run it like this “forever start app.js –prod”. when I open up my browser (http:127.0.0.1:1337), nothing shows up.

Answer

The answer, is a new “safety switch” that has been added to migrations. This safety switch protects you from accidentally erasing precious data from your database when changing the properties of models due to database errors. What happens, is that sails asks you a question, that requires user input to be answered and go away (continue the flow). Using forever, this never happens. The message sails gives you is:

Excuse my interruption, but it looks like this app does not have a project-wide “migrate” setting configured yet. (perhaps this is the first time you’re lifting it with models?)

And gives you the following options:

  1. safe – never auto-migrate my database(s). I will do it myself (by hand)
  2. alter – auto-migrate, but attempt to keep my existing data (experimental)
  3. drop – wipe/drop ALL my data and rebuild models every time I lift Sails

To fix this, all you have to do is add a default migrate option in config.models.js. You could, for instance, simply add safe:

Q&A: autoWatch false still pushes updates sails.js

Question

The autoWatch configuration option for blueprints seems to be causing some confusion every now and then. Specifically, the question is:

Why, after disabling autoWatch by setting it to false, do I still receive updates from pubsub?

Answer

Taken from the sails documentation, the option autoWatch allows you to specify:

Whether to run Model.watch() in the find and findOne blueprint actions. Can be overridden on a per-model basis.

The function Model.watch() watches for newly created records for that specific model; not updates. The subscribe method however, is responsible for updates.

Found here, you can see sails uses a different option for pubsub:

If you want to disable pubsub, you can read how to do that on disabling pubsub in sails.

Q&A: Disable pubsub sails.js

I love short, concise blog posts. Let this be one of them!

Question

After having had this question asked a couple of times, I decided it qualified for a dedicated blog post. This particular question has been asked on IRC, a while back on github and in a sense, as well on StackOverflow.

[10:29:53] RWOverdijk: do u know if there is a way to completely shut off the pubsub feature in blueprints?

Answer

In your project’s root, create a file called .sailsrc. Inside of this file, add the following:

Voila, you’ve disabled pubsub.

Javascript dependencies

The never ending journey.

As a lot of you might have had as well, I’ve had a lot of problems with managing my javascript dependencies. There are a lot of ways you can do it, but most of them either require a lot of work, or have too many dependencies to work with.

I’ve searched long and hard, trying to find the best solution. I’ve come across Require.js, browserify, grunt and many more, but these were the best solutions in the bunch. Here are some of my findings on these three.

Require.JS

Require.JS is different in that it’s mainly meant to do AMD loading. It allows you to do some cool things, and actually works quite well. The problems I have with require.js are as follows:

  • It forces you to wrap your code in a function call.
  • It forces another (quite large) dependency on you.
  • It’s difficult to add non-AMD dependencies.
  • When a non-AMD dependency requires the use of another non-AMD file, good luck.
  • When you want to add unit tests you’ll have to work around require.js.

It’s good, but not good enough for me.

Browserify

Browserify looks very appealing when you look at it for the first time. Using require calls in my front-end javascript!? Yes please! Unfortunately, after trying to work around it for quite a while, I’ve found a couple of pretty upsetting drawbacks:

  • It’s a bit fragile and doesn’t always do what you’d expect.
  • It makes generating code coverage a tad difficult (it doesn’t recognize the require calls).
  • It forces a dependency on you.

It’s better than require.js, but still not good enough.

Grunt (concat, usemin)

GruntJS, the task-running bamf. It’s amazing in that grunt itself doesn’t manage your dependencies for you, the plugins do. So this is actually a comparison between two of the best plugins I’ve found.

usemin

Usemin allows you to add good old script tags to your html, and html comments to figure out where to put the combined file. This is pretty cool, but it’s only cool when using static html files as it rewrites your html for you. I was able to still implement this, have it write my views to a different directory and tell my app to use those views. This worked okay, but still felt a bit weird and dirty.

concat

grunt-contrib-concat, the best I’ve found. This plugin is really straight forward. Specify the javascript sources, and where (what file) you’d like to put them. It just concatenates a list of scripts, and allows you to supply wildcards, too (the way grunt does). This is super simple, and actually is all anyone would ever need. Downside? It requires quite a bit of extra work as you’ll always have to update the task in the gruntfile whenever you add a new dependency and also forces you to keep a mapping between those files and the gruntfile so you don’t end up with orphans.

In comes useuses

Tired of looking through all these alternatives I decided it was time to write something myself. I called it useuses because it allows you to use uses. Aren’t I creative?

What it is.

Before I started writing this plugin, I assembled a list of requirements:
* It must be optional.
* It mustn’t force a dependency upon someone.
* It must be “standard”, so no weird tags.
* It must add value to the files, even when not used.

With those requirements set, I came up with a way to specify what dependencies a file has, to make it informative for the developers. So as soon as you were to open up a file, you’d immediately see what it depends on. This made me think of the way jquery ui does that. It’s nice, but not nice enough. It’s still unformatted information.

The syntax

I finally came up with the following syntax:

This is informative and standardized in the docblock format. I immediately added support some other formats:

Conclusion

I liked all alternatives and think that the developers did a great job creating solutions. I think that whatever you decide to use depends on your project’s requirements and development style.

If you wish to give useuses a go, you can find the npm package for useuses by clicking here.

Happy coding.

Zend Framework 2 modules

About a week ago SpiffyJr created a post on his blog outlining his modules and their state. I quite liked the initiative, and felt like following up on this (what I hope to become) trend.

The reason I feel this is a good idea, is because there seems to be a lot of duplicate module creation. Zend Framework 2 developers don’t always realise something already exists, so decide to build something themselves. This is a waste of time, efforts and talent, which are all things I hate wasting. For this reason, I’ve compiled my own list of modules. So following is a description of my modules.

Modules

Most of my modules reside under the SpoonX organisation. The modules that make it to the organisation are stable, or at least complete enough. Currently, the only module that’s stable but not under the spoonx namespace, is the AssetManager module.


AssetManager

Summary

Description

As perfectly described in the readme:

This module provides functionality to load assets and static files from your module directories through simple configuration. This allows you to avoid having to copy your files over to the public/ directory, and makes usage of assets very similar to what already is possible with view scripts, which can be overridden by other modules. In a nutshell, this module allows you to package assets with your module working out of the box.

On top of this, the module also allows you to apply filters, apply caching, create collections, create custom resolvers, create custom filters and create custom cachers.


SxBootstrap

Summary

Description

This module is a wrapper for twitter bootstrap. It comes with some neat features allowing you to do some cool things.

Features (as taken from the README.md):

  • Simple awesome forms (group action controls, create horizontal forms etc)
  • Fast
  • Customisable (variables, components, plugins. No more downloading zip files!)
  • Works with the AssetManager
  • Plenty of ViewHelpers
  • Extendable (run your own less files from bootstrap context to share mixins and variables)

SxBootstrapWizard

Summary

Description

There’s not much to this module. It’s a plugin for SxBootstrap that allows you to easily create stunning wizards.


SxCore

Summary

Description

This is (currently) a very small library. It has two components.

SxCore\Html\HtmlElement

The HtmlElement is pretty neat, it allows you to easily create markup using php. This is especially useful when creating viewhelpers; which is the reason it’s being used by SxBootstrap so intensively.

I started writing this component because of the annoying way Zend\Form\Element were written, and the restrictions they had. I was constantly messing around with strings (eww) and found myself unable to add ng- attributes due to restrictions (which, if you’ve ever used angular, is extremely annoying).

You can find some examples here.

SxCore\Html\ExcerptExtractor

This neat little component allows you to extract excerpts from bundles of markup without breaking the tags. This is really useful when it comes to, I don’t know, writing a blog (Yes, I’m hinting at SxBlog).


SxMail

Summary

Description

This module makes it incredibly easy to set up mailings. It does all of the boring work for you, and supplies useful functionality such as using ViewModels as templates and wrapping those templates in a layout. It also allows you to set up multiple configurations for multiple mailings, which will be merged with the default configuration (marked by the “default” key).

Features (as taken from the README.md):

  • Easily configurable. Both messages and transport.
  • Use ViewModels
  • Layout support
  • Mimepart detection
  • Easy header configuration
  • Access to Zend* classes
  • Generates alternative (text) body
  • 100% code coverage
  • Scratches your back after a long day of work
  • Makes a good drinking buddy.

SxBlog

Summary

Description

This module is an attempt at creating a skeleton blog for minimalist bloggers (such as myself). It already includes a handful of features, but isn’t complete yet.

Here’s a screenshot of it in use:


SxSkeletonModule

Summary

Description

This is not really a module. It’s basically a skeleton module (yes, I know, the repository name sort of gave that away) that allows you to easily start a new Zend Framework 2 module. It includes a license, composer.json, a module structure and basic tests. It’s easily used by running the setup script that’s bundled with it.

The following is the easiest way to get this up and running: git clone git@github.com:SpoonX/SxSkeletonModule.git; cd SxSkeletonModule; ./setup.sh Username ModuleName; cd ..;

Note: Make sure you change the Username and ModuleName to yours. It’s used for the composer.json (and more files).

Conclusion

Writing this post took me quite a while but it was worth it. I hope it’ll help a developer or two. Building modules is awesome; but nothing’s as satisfying as watching people use, and enjoy using them.

There are obviously a lot more modules out there. A while ago Hounddog created a Zend Framework 2 modules application for which I helped out with the styling. It’s a really neat resource if you’re looking for modules, and I strongly suggest you submit your modules, too!

Happy coding!

zend framework 2 blog module

Dear WordPress,

After many years of fiddling with you, it seems I have finally reached my breaking point. It started off as exciting, hot, heavy and fun, like most relations do, but it has become an annoyance to deal with you. The resources you ask of me, the many confusing options you present me with… It’s just too much. I can’t deal with it anymore, and I want to be able to be myself again! I’ve had it with adapting to your needs just so I won’t hurt your feelings.

So it seems that the time has come for me to build my own blog based on Zend Framework 2. I’ve already started with the module and it has been a joy working on it so far. For now, it includes the following features:

  • An overview of posts
  • An overview of categories
  • An overview of posts by category
  • Pagination
  • Create new post
  • Delete post
  • Edit post
  • View post
  • Create new category
  • Delete category
  • Edit category
  • Customisable messages (for deletion, creation, updates etc)
  • Theme support (really just by supplying view files. Extended support will follow)

The module can be found here: https://github.com/SpoonX/SxBlog

There’s more to come, but for now this is more than enough for me to leave you. It’s just younger, more organised and it listens to me. And in the end, it was ‘The Loop‘ that drove me away from you…

It’s not me, it’s you. But you will forever hold a place in my memory. I wish you the best of luck. in the years to come.

No longer yours,

Roberto Wesley Overdijk.

PHP sandbox online editing

I’m guessing you got to this page because you like PHP, and you like testing without creating entire directory structures or using php -r ”;.

Well, me too. That’s why I went out trying to find a tool that could help me with exactly that. One problem! There were none. And the ones that I did find also did not work the way I wanted them to. They were either online, too bloated, or required ftp access.

So, I built one. Yes, yes I did. You can find the code here: https://github.com/RWOverdijk/SxPhandbox

Installation should be pretty straight forward, as explained in the readme.

Runtime errors in the editor

Have fun!

Redirecting from a controller in ZF2

Hey all. Today’s post is a response to a question asked today on IRC.

The question:

In zf1 within a controller I could do something like $this->getResponse()->setRedirect(‘/foo/bar’, 201) ~ How do I do that using zf2?

The answer:

Controllers have plugins, not much has changed there. Except for the fact that it’s now even easier to get a redirect going. Check this out:

$this->redirect()->toRoute('routeName');

It also has a toUrl method, which behaves the same way:

$this->redirect()->toUrl('http://google.nl/');

Counter question:

Can you send a statusCode with that though?

Counter answer:

Yes, you can. As seen here, the redirect returns a Response. A response has a setStatus code, thus allowing us to do this:

$this->redirect()->toRoute('routeName')->setStatusCode(201);

Happy coding 🙂

 

Asset manager module for zend framework 2

Not very long ago I published the first stable version of the AssetManager module for Zend Framework 2. The purpose of the module is to allow you to package assets with your module, in stead of having to mess around with symbolic links, aliasmatch or mod_rewrite. On top of that, this module also gives you some other functionality such as caching, grouping assets and applying filters to your assets. You can add custom filters, custom cachers and custom asset resolvers.

https://github.com/RWOverdijk/AssetManager

©SpoonX 2017