use jquery when then instead of nested function in space.js