PDA

View Full Version : [CLOSED] StoreDataHandler Refresh



ehmdb
Aug 13, 2013, 5:30 PM
I have a StoreForModel which has the following SyncUrl:


.Store(X.StoreForModel().ID("FeatureStore")
.AutoSync(true)
.ShowWarningOnFailure(true)
.Listeners(l =>
{
l.Exception.Fn = "onStoreException";
})
.SyncUrl(Url.Action("HandleFeatureChanges"))
.PageSize(15)
)

public ActionResult HandleFeatureChanges(StoreDataHandler handler)
{
List<FeatureViewModel> records = handler.ObjectData<FeatureViewModel>();
User user = UserHelper.GetUser();

#region db update
if (handler.Action == StoreAction.Create)
{
foreach (FeatureViewModel created in records)
{
created.Add(created, user);
}
}
else if (handler.Action == StoreAction.Destroy)
{
foreach (FeatureViewModel deleted in records)
{
//FeatureViewModel.Delete(deleted.FeatureIDN);
}
}
else if (handler.Action == StoreAction.Update)
{
foreach (FeatureViewModel updated in records)
{
updated.Update(updated, user);
updated.UpdateDate = DateTime.Now;
}
}
#endregion

List<FeatureViewModel> dbRecords = FeatureViewModel.FeatureViewModelFromFeature(_db.F eatures.ToList());

// DOES NOT WORK
var store = this.GetCmp<Store>("FeatureStore");
store.LoadData(dbRecords);

// DOES NOT WORK
//return handler.Action != StoreAction.Destroy
// ? (ActionResult)this.Store(dbRecords)
// : (ActionResult)this.Content("");

return handler.Action != StoreAction.Destroy
? (ActionResult)this.Store(records)
: (ActionResult)this.Content("");
}

I would like to refresh the Store with data from a database, not the data submitted in form. This way I can get newly added primary keys and database defaults. Please advise.

Baidaly
Aug 14, 2013, 1:45 AM
Hello!

It seems that you cannot load new data or call JS if you call handler to update, delete or create records. Can you say your scenario?

You can load data manually using DirectEvent. The following sample based on this sample: http://mvc.ext.net/#/GridPanel_Update/AutoSave/

View:


@model IEnumerable<Ext.Net.MVC.Examples.Areas.GridPanel_Update.Models .TestPerson>

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_BaseLayout.cshtml";
}ur s

@section headtag
{
<script>
var updateRecord = function (form) {
if (form.getForm()._record == null) {
return;
}

if (!form.getForm().isValid()) {
Ext.net.Notification.show({
iconCls : "icon-exclamation",
html : "Form is invalid",
title : "Error"
});
return false;
}

form.getForm().updateRecord();
};

var addRecord = function (form, grid) {
if (!form.getForm().isValid()) {
Ext.net.Notification.show({
iconCls : "icon-exclamation",
html : "Form is invalid",
title : "Error"
});

return false;
}

grid.store.insert(0, new Person(form.getForm().getValues()));
form.getForm().reset();
};

var onStoreException = function (propxy, response, operation) {
var error = operation.getError(),
message = Ext.isString(error) ? error : ('(' + error.status + ')' + error.statusText);

Ext.net.Notification.show({
iconCls : 'icon-exclamation',
html : message,
title : 'EXCEPTION',
autoScroll : true,
hideDelay : 5000,
width : 300,
height : 200
});
};
</script>
}

@section example
{
<h1>Grid with AutoSave</h1>
<p>An Error has been simulated on the server-side: Attempting to update a record having ODD-numbered id will generate this errror. This error can be handled by listening to the "exception" event upon your Store.</p>
<script>
var onSuccess = function (grid, data) {
grid.show();
grid.getStore().loadData(data);
};
</script>
@(
Html.X().FormPanelForModel()
.ID("UserForm")
.Icon(Icon.User)
.Frame(true)
.Title("User -- All fields are required")
.Width(500)
.DefaultAnchor("100%")
.FieldDefaults(d => {
d.LabelAlign = LabelAlign.Right;
})
.Buttons(
Html.X().Button()
.Text("Save")
.Icon(Icon.Disk)
.Handler("updateRecord(this.up('form'));"),

Html.X().Button()
.Text("Create")
.Icon(Icon.UserAdd)
.Handler("addRecord(this.up('form'), App.GridPanel1);"),

Html.X().Button()
.Text("Reset")
.Handler("this.up('form').getForm().reset();")
)
)

@(
Html.X().GridPanel()
.ID("GridPanel1")
.Store(
Html.X().StoreForModel()
.AutoSync(true)
.ShowWarningOnFailure(false)
.Listeners(l => {
l.Exception.Fn = "onStoreException";
})
.SyncUrl(Url.Action("HandleChanges"))
)
.Icon(Icon.Table)
.Frame(true)
.Title("Users")
.Height(400)
.Width(500)
.StyleSpec("margin-top: 10px;")
.ColumnModel(
Html.X().ColumnFor(Model, m => m.Id)
.ToBuilder<Column.Builder>()
.Width(40),

Html.X().ColumnFor(Model, m => m.Email)
.ToBuilder<Column.Builder>()
.Flex(1)
.Editor(
Html.X().TextField().AllowBlank(false).StandardVty pe(ValidationType.Email)
),

Html.X().ColumnFor(Model, m => m.First)
.ToBuilder<Column.Builder>()
.Flex(1)
.Editor(
Html.X().TextField().AllowBlank(false)
),

Html.X().ColumnFor(Model, m => m.Last)
.ToBuilder<Column.Builder>()
.Flex(1)
.Editor(
Html.X().TextField().AllowBlank(false)
),

Html.X().CommandColumn()
.Width(70)
.Commands(
Html.X().GridCommand()
.Text("Reject")
.ToolTip(t => {
t.Text = "Reject row changes";
})
.CommandName("reject")
.Icon(Icon.ArrowUndo)
)
.PrepareToolbar(t=>{
t.Handler = "toolbar.items.get(0).setVisible(record.dirty);";
})
.Listeners(l => {
l.Command.Handler = "record.reject();";
})
)
.TopBar(
Html.X().Toolbar()
.Items(
Html.X().Button()
.Text("Load Data")
.DirectEvents(de =>
{
de.Click.Url = Url.Action("GetData");
de.Click.Success = "onSuccess(App.GridPanel1, result.data);";
}),
Html.X().Button()
.Text("Add")
.Icon(Icon.Add)
.Handler("this.up('grid').store.insert(0, new Person());"),

Html.X().Button()
.Text("Delete")
.Icon(Icon.Exclamation)
.Handler("this.up('grid').deleteSelected(); App.UserForm.getForm().reset();"),

Html.X().ToolbarSeparator(),

Html.X().Button()
.Text("Auto Sync")
.EnableToggle(true)
.Pressed(true)
.ToolTip("When enabled, Store will execute Ajax requests as soon as a Record becomes dirty.")
.ToggleHandler("function(b, pressed){this.up('grid').store.autoSync = pressed;}")
)
)
.SelectionModel(
Html.X().RowSelectionModel()
.Mode(SelectionMode.Single)
.Listeners(l => {
l.Select.Handler = "App.UserForm.getForm().loadRecord(record);";
})
)
.Buttons(
Html.X().Button()
.Text("Sync")
.Icon(Icon.Disk)
.Handler("this.up('grid').store.sync();")
)
.Plugins(
Html.X().CellEditing()
)
)


Added controller method:


public ActionResult GetData()
{
return new StoreResult(TestPerson.TestData.Where(p => p.Id % 2 != 0));
}