Text

Introducing Injector - Dependency Injection for Node

tl;rd

Introducing: Injector - Simple dependency injection for Nodejs

Introduction

These days, modules have become a household name and NPM has become the Walmart of Nodejs (or maybe the Target). That’s fantastic, except that it’s a pain to generate a new module, set it up, publish it, and then require it in your current project (but not really, but it is, but not really - http://substack.net/how_I_write_modules).

Read More

Text

Function wrapper to avoid creating a new deferred in every function using the Q (http://documentup.com/kriskowal/q) library.

Function wrapper to avoid creating a new deferred in every function using the Q (http://documentup.com/kriskowal/q) library.

Text

Highly Optimized Javascript Constructor

So often we are looking for speed in the interface of our web apps. But, we forget that speed doesn’t matter if the browser crashes because our app becomes a memory hog. Thanks to compiled Coffeescript, I was able to devise an efficient way to create and inherit from constructors in Javascript.

var Parent = (function() {
    var Parent = {},
        parentPrototype = {
            func1: function(){}
        };

    Parent.create = function() {
        var par = Object.create(parentPrototype);

        return par
    };

    return Parent;
})();

This technique allows us to declare a prototype object and create() function for inheritance, and use scope within a closure to allow all objects created with this constructor to share the same memory location for it’s inherited prototype.

Here’s the same constructor on JSFiddle 

Would love some feedback!

Text

MySQL said: Cannot add or update a child row: a foreign key constraint fails

When working with MySQL InnoDB relationships, I ran into a snag when dealing with foreign keys. I was attempting to creating a cascading relationship between a “projectID” and the “ID” field of the “projects” table. BUT, I kept getting an annoying error stating: “MySQL said: Cannot add or update a child row: a foreign key constraint fails”.

After 30 minutes of failed Google searches, I finally figured out the issue myself.

Problem:

Attempting to create a foreign key cascade relationship between tables failed. MySQL simply said NO, Scott, you’re DUMB!

Solution:

I was attempting to relate a foreign key in one table to a primary key in another table where a matching row did not exist. In order to create a cascading foreign key relationship from one table to the next, you cannot have records in the foreign table without a corresponding record in the primary table.

For example: if I have a taskList with a foreign key for “projectID” with a value of 3, I must have a record in the table for “projects” with a matching “ID” of 3.

Hope this helps you keep your hands out of your hair!

Tags: mysql database
Text

Backbone.js Collection Creates Blank Model When it is Initialized

If you’ve ever had an issue with a blank Backbone model showing up every time you initialize a collection, I may have found your problem.

It seems that when you initialize a Backbone collection, the collection does not handle the object you pass the initialize() method the  same way that a model or a view does. A Backbone collection, minus a few keywords (model, etc.), will automagically try to initialize a new model with the object you are passing it’s initializer. See the solution below:

Problematic Code:

// in a view, or wherever
this.collection = new Collection({
  url : 'http://someurl.com',
  anotherOption : 'This is another option'
});

Solution:

// in a view, or wherever
this.collection = new Collection;
this.collection.url = 'http://someurl.com';
this.collection.anotherOption = 'This is another option';

Please let me know if you’ve found a better solution!

Text

Backbone.js model.save() creates POST request instead of a PUT request while using MongoDB

I could not, for the life of me, figure out why Backbone.js was sending a POST request to my server rather than a PUT request while I was trying to save a model that had already been created. 

I am using Node.js on the back-end, Backbone.js on the front-end, and MongoDB as my database. I finally found a few solutions and it made it ALL work!

Here was my problem:

Backbone.js model.save() creates POST request instead of a PUT request while using MongodDB

Here is solution 1:

This sets the default place that Backbone looks for the id of the model


var Model = Backbone.Model.extend({
  idAttribute : '_id'
});

Here is solution 2:

This uses the set function of the model to set the attribute to either ‘_id’ or ‘id’, depending on which one is already set


var Model = Backbone.Model.extend({
  initialize : funcition(opts){
    this.set({
      id : attributes['_id'] || attributes['id']
    });
  }
});

Text

Add and Save Multiple Items to Shopping Cart with WP E-Commerce

When developing a custom e-commerce site for a client recently, I ran into a huge issue with WP E-Commerce. There’s NO WAY to easily add multiple items to their shopping cart, at once!

Well, you’re looking for a solution to adding multiple items to a shopping cart with WP E-Commerce, here’s an ajax solution:


var function_callback = function(result) { 
 // callback code goes here 
}
function add_item_to_cart(product_id,quantity) {  
  /* set default for product quantity to 1 */ 
  if(!quantity){ quantity = 1 }  

  /* query string to send wordpress to tell it to add these options to the cart */ 
  var data = "custom_text=&wpsc_quantity_update="+ quantity +"&key=-1&wpsc_update_quantity=true&wpsc_ajax_action=add_to_cart&product_id="+ product_id +"&is_customisable=true";  

  /* uses custom function from WP E-Commerce to update cart with ajax */ 
  ajax.post('index.php?ajax=true', function_callback, data);
}

To use the code above, all you need to do is send the function add_item_to_cart the product_id and the quantity of that product. With some Ajax/Wordpress/WP E-Commerce magic, you will have your shopping cart automatically updated.

And…. that’s it! You can now call this function at will, at any time. Please, feel free to ask me any questions about this product in the comments!

Posted via email from Scott Corgan | Comment »

Text

New Scottcorgan.com Launch Update

It seems as though I overlooked a small error with a plugin. I am fixing some code, then it’s time for the final countdown. Thanks for your patience!

Posted via email from Scott Corgan | Comment »

Text

Daily Free @font-face Ready Font #1

Text

Getting Ready To Launch The New Scottcorgan.com

As I type this, I’m am uploading the new files to scottcorgan.com. In about an hour, the new design will launch. Very excited for this!

Posted via email from Scott Corgan | Comment »