Skip to content

Instantly share code, notes, and snippets.

View 1Marc's full-sized avatar

Marc Grabanski 1Marc

View GitHub Profile
// my little html string builder
buildHTML = function(tag, html, attrs) {
// you can skip html param
if (typeof(html) != 'string') {
attrs = html;
html = null;
}
var h = '<' + tag;
for (attr in attrs) {
if(attrs[attr] === false) continue;
@1Marc
1Marc / titanium-desktop-dropbox-upload
Created June 23, 2011 00:09
Trying to Upload a file to Dropbox through Titanium Desktop
// relevant dropbox threads:
// - http://forums.dropbox.com/topic.php?id=28728&replies=12#post-
// - http://forums.dropbox.com/topic.php?id=31832
// code depends on spazcore.titanium.js (found in Tweetanium)
// SIMPLE WORKING EXAMPLE
var oa = new SpazOAuth("https://api.dropbox.com/", {"consumerKey": DROPBOX_KEY, "consumerSecret": DROPBOX_SECRET});
oa.setAccessToken(ACCESS_TOKEN, ACCESS_SECRET);
@1Marc
1Marc / gist:1043311
Created June 23, 2011 18:59
latest code
var uploadFile = Titanium.Filesystem.getFile(filepath, filename);
var uploadStream = Titanium.Filesystem.getFileStream(uploadFile);
uploadStream.open(Titanium.Filesystem.MODE_READ, true);
content = uploadStream.read();
uploadStream.close();
// content = Titanium.Codec.encodeBase64(content);
// content = Titanium.Codec.encodeHexBinary(content);
var name = "file";
@1Marc
1Marc / fn arrEach and objEach
Created July 25, 2011 05:25
"But $.each is slow!" ..use fn arrEach and objEach then!!
// arrEach and objEach plugins
// code under MIT license by Marc Grabanski http://marcgrabanski.com
// jsperf tests: http://jsperf.com/each-vs-fn-arreach-and-objeach/2
$.arrEach = function(arr, cb){
for (var i = 0, item; item = arr[i]; ++i) {
cb.apply(item, [i, item]);
}
return arr;
}
@1Marc
1Marc / gist:1132317
Created August 8, 2011 18:02
custom event proxy to custom template component
.bind('xgcolor:hsbmove' + ns, $.proxy(function(e) {
var pos = $(this).offset(),
hsbcolor = $.xgcolor.options.hsbcolor;
hsbcolor.b = parseInt(100*(126 - Math.max(0,Math.min(126,(e.pageY - pos.top))))/126, 10);
hsbcolor.s = parseInt(100*(Math.max(0,Math.min(130,(e.pageX - pos.left))))/130, 10);
$.xgcolor._setColor(hsbcolor);
}, this.get('hsbselector')))
@1Marc
1Marc / backboneview.$find
Created July 10, 2012 22:49
Cleaning up finding elements in Backbone Views
Add this to your backbone view:
selectors: {
form: '.ui-page-active form',
backbutton: '.ui-page-active .back',
...all your selectors...
},
$find: function(key) {
if (this.selectors.hasOwnProperty(key)) {
@1Marc
1Marc / gist:3086752
Created July 10, 2012 22:52
Cleaning up finding elements in Backbone Views
// Add this to your backbone view:
selectors: {
form: '.ui-page-active form',
backbutton: '.ui-page-active .back',
...all your selectors...
},
$find: function(key) {
if (this.selectors.hasOwnProperty(key)) {
@1Marc
1Marc / _.md
Created October 26, 2012 17:23
just another inlet to tributary
@1Marc
1Marc / gist:4770007
Last active December 12, 2015 12:08
Pseudocode demonstrating inefficient rendering
// Pseudocode demonstrating inefficient rendering
var $list = $('#list');
var dataset = data; // array of 1000 objects
for (var i = 0; i < dataset.length; i++) {
var view = new ItemView({model: dataset[i]});
$list.append(view.render().el);
}
@1Marc
1Marc / gist:4770143
Last active December 12, 2015 12:09
Pseudocode demonstrating pre-rendering HTML and binding views to it rather than the views generating HTML.
// Pseudocode demonstrating pre-rendering HTML
// Binding views to HTML rather than the views generating HTML
var $listItems = $('#list').find('li');
var dataset = data; // array of 1000 objects
for (var i = 0; i < dataset.length; i++) {
var view = new ItemView({model: dataset[i]});
view.setElement($listItems.eq(i));
}