Last active
August 29, 2015 14:19
-
-
Save AndrewHenderson/a5f403039386dc4efebc to your computer and use it in GitHub Desktop.
Transaction Sets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// REQUEST | |
// transactionSets POST | |
// transactionSets/23 PUT | |
{ | |
transactionIds: [315, 311] | |
} | |
// RESPONSE | |
{ | |
currentTransactionSet: { | |
mergeId: 23, | |
primaryTransactionId: 315, | |
transactionIds: [315, 311, 300] | |
}, | |
updatedtedTransactionSets: [{ | |
mergeId: 13, | |
primaryTransactionId: 89, | |
transactionIds: [89, 91] | |
}], | |
deletedTransactionsSetMergeIds: [47] | |
} | |
// TRANSACTIONS & MERGE SETS API | |
var cache = { | |
transactions: [], | |
mergeSets: [] | |
} | |
function fetchMergeSets(){ | |
var defer = $.Deferred(); | |
$.ajax({ | |
url: "transactions", | |
parse: function(res){ | |
cache.transactions = res.transactions; | |
cache.mergeSets = res.mergeSets; | |
defer.resolve( createMergeSetCollection(res) ); | |
} | |
}); | |
return defer; | |
} | |
function createMergeSetCollection(res){ | |
var unmerged = _.filter(res.transactions, function(transaction){ | |
return transaction.mergeId === undefined; | |
}); | |
var mergeSets = res.mergeSets; | |
_.each(unmerged, function(transaction){ | |
mergeSets.push({ | |
primaryTransactionId: transaction.id, | |
transactionIds: [transaction.id] | |
}); | |
}); | |
var mergeSetCollection = new MergeSetCollection(); | |
return mergeSetCollection.add(mergeSets); | |
} | |
function mergeSetUpdate(attrs) { | |
var model = cache.mergeSets.get(attrs.id); | |
if( attrs.transactionIds.length =< 1 ) { | |
model.set({ | |
id: undefined, | |
primaryTransactionId: undefined | |
}); | |
} | |
model.set({transactionIds: attrs.transactionIds}); | |
} | |
function createTransactionCollection(ids){ | |
var transactions = _.filter(cache.transactions, function(transaction){ | |
return _.contains(ids, transaction.id); | |
}); | |
var transactionCollection = new TransactionCollection(); | |
return transactionCollection.add(transactions); | |
} | |
Data.reply("mergeSets:fetch", function(){ | |
return fetchTransactions(); | |
}); | |
Data.comply("mergeSet:change", function(attrs){ | |
mergeSetUpdate(attrs); | |
} | |
Data.reply("transaction:collection:create", function(ids){ | |
return createTransactionCollection(ids); | |
}); | |
// TRANSACTIONS COLLECTION | |
url: "transactions" // PUT, DELETE | |
model: TransactionModel | |
// MERGE SET COLLECTION | |
url: "mergeSet", // POST, PUT | |
model: MergeSetModel | |
// MERGE SET MODEL | |
defaults: { | |
transactionIds: [] | |
}, | |
parse function(res) { | |
if(res.affectedMergeSets){ | |
Data.command("mergeSet:primaryTransaction:change", res.otherModel); | |
} | |
return res.currentModel; | |
} | |
// CONTROLLER | |
Data.request("mergeSets:fetch").done(function(collection){ | |
mergeSetCollectionView = new MergeSetCollectionView({ | |
collection: collection | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment