![]() |
| Ending Never Staircase |
Any SproutCore devs comment?
![]() |
| Ending Never Staircase |
namespace SproutCoreMVC.Models
{
public class Task
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public string description { get; set; }
public int order { get; set; }
public bool isDone { get; set; }
public string guid { get {return "/tasks/" + id;} }
}
public class SCModels : DbContext
{
public DbSet<Task> Tasks { get; set; }
}
public class SCModelsInitializer : DropCreateDatabaseIfModelChanges<SCModels>
{
protected override void Seed(SCModels context)
{
var tasks = new List<Task>
{
new Task {
order = 1,
isDone = false,
description = "Some Description"
}
};
tasks.ForEach(d => context.Tasks.Add(d));
context.SaveChanges();
}
}
}This is the REST implementation. Easy da?
namespace SproutCoreMVC.Controllers
{
public class TasksController : Controller
{
SCModels db = new SCModels();
// GET: /tasks
[HttpGet]
[NoCache]
public ActionResult List()
{
return Json(new {content = db.Tasks}, JsonRequestBehavior.AllowGet);
}
//GET: /tasks/id
[HttpGet]
[NoCache]
public JsonResult Get(int id)
{
return Json(db.Tasks.Find(id), JsonRequestBehavior.AllowGet);
}
//PUT: /tasks/id
[HttpPut]
public ActionResult Update(Task task)
{
var taskToUpdate = db.Tasks.Find(task.id);
UpdateModel(taskToUpdate);
db.SaveChanges();
return Json(taskToUpdate);
}
//POST: /tasks
[HttpPost]
public ActionResult Create(Task task)
{
db.Tasks.Add(task);
db.SaveChanges();
//set location and to redirect headers
Response.StatusCode = 201;
Response.RedirectLocation = task.guid;
return new EmptyResult();
}
//DELETE: /tasks/id
[HttpDelete]
public ActionResult Delete(int id)
{
db.Tasks.Remove(db.Tasks.Find(id));
db.SaveChanges();
return new EmptyResult();
}
}
} I left out the routing information. As you can see the integration between SproutCore and asp.net MVC3 and Entity Framework 4 is quite straightforward and painless. I actually only write about 70 lines of code. Is much easier than you half of project.
![]() |
| Beautiful Anastasia Volochkova |
mainView : SC.TabView.design({
layout : {top : 36, left : 40, right : 40, bottom : 40},
nowShowing : 'otherStepsView',
itemTitleKey : 'title',
itemValueKey : 'value',
itemIconKey : 'icon',
itemActionKey : 'action',
itemTargetKey : 'target',
items : [ {
title : 'Steps',
value : 'stepsView',
icon : 'sc-icon-folder-16'
},
{ title: 'otherSteps',
value: 'otherStepsView',
icon: 'sc-icon-folder-16',
action: 'someAction',
target: 'MyApp.aController'
}
]
})
someAction: function(something){
console.log('You clicked me! I am: '+something);
console.log('You expect me to have a value of "otherStepsView" but surprise: I am: '
+something.get('value'));
var theOtherView = MyApp.mainPage.get('otherStepsView');
MyApp.mainPage.mainPane.mainView.set('nowShowing', theOtherView);
return true;
}
namespace SproutCoreMVC.Models
{
public class Task
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public string description { get; set; }
public int order { get; set; }
public bool isDone { get; set; }
public string guid { get {return "/tasks/" + id;} }
}
public class SCModels : DbContext
{
public DbSet<Task> Tasks { get; set; }
}
public class SCModelsInitializer : DropCreateDatabaseIfModelChanges<SCModels>
{
protected override void Seed(SCModels context)
{
var tasks = new List<Task>
{
new Task {
order = 1,
isDone = false,
description = "Some Description"
}};
tasks.ForEach(d => context.Tasks.Add(d));
context.SaveChanges();
}
}
}This is the REST implementation. Easy da?
namespace SproutCoreMVC.Controllers
{
public class TasksController : Controller
{
SCModels db = new SCModels();
// GET: /tasks
[HttpGet]
[NoCache]
public ActionResult List()
{
return Json(new {content = db.Tasks}, JsonRequestBehavior.AllowGet);
}
//GET: /tasks/id
[HttpGet]
[NoCache]
public JsonResult Get(int id)
{
return Json(db.Tasks.Find(id), JsonRequestBehavior.AllowGet);
}
//PUT: /tasks/id
[HttpPut]
public ActionResult Update(Task task)
{
var taskToUpdate = db.Tasks.Find(task.id);
UpdateModel(taskToUpdate);
db.SaveChanges();
return Json(taskToUpdate);
}
//POST: /tasks
[HttpPost]
public ActionResult Create(Task task)
{
db.Tasks.Add(task);
db.SaveChanges();
//set location and to redirect headers
Response.StatusCode = 201;
Response.RedirectLocation = task.guid;
return new EmptyResult();
}
//DELETE: /tasks/id
[HttpDelete]
public ActionResult Delete(int id)
{
db.Tasks.Remove(db.Tasks.Find(id));
db.SaveChanges();
return new EmptyResult();
}
}
} ![]() |
| I nearly use Thermonuclear option. |
var users = MyApp.store.find('MyApp.User'));Da, easy. Find all users. Show into SC.ArrayController. Go eat borscht. Drink Vodka.
MyApp.usersController.set('content', users);
....
allUsers: SC.SourceListView.design({
... some stupid layout
contentBinding: 'MyApp.usersController.arrangedObjects'
canEditContent: YES,
canReorderContent: YES,
delegate: MyApp.mustMakeDelegate
}),
usersILike: SC.SourceListView.desig({
...
delegate: MyApp.mustMakeDelegate,
isDropTarget: YES // what else?
})Whooo wheee now I reload page and drag and drop contents right? Sookin Syn!
var users = SC.A(MyApp.store.find('MyApp.User')));Alexei say SC.A is not short for 'SproutCore.Assholes: have a good time figureing this out', but I'm not sure.
MyApp.usersController.set('content', users);
![]() |
| Da, is serious question! |
![]() |
| Real MotoCycle: Handlebars! |
<h1>Test Handlebars Page</h1><p>I really like {{ somevar }}!</p>
MyApp.TestView = SC.ScrollView.extend({render: function(context) {// Note: Do not use the word 'view' as a key in the following data object,// as SproutCore and/or Handlebars will not display it properly// (probably because it's a helper function)var data = { somevar:"SproutCore" };var template = SC.TEMPLATES['test'](data);context.push( template );},update: function(jquery) {// do whatever you need to for somevar...}});