4

I am trying to create a SPA application, to take advantage of upshot and its capabilities. However, the services exposing the data (i.e. the Web API) is in a completely separate project, already hosted on IIS. I would like to bind upshot to that existing API, but from what I can see in the HTML helpers for upshot (v1.0.0.1), you need to pass in the type of the controller that exposes the data, like so:

Html.UpshotContext.DataSource(Of ToDoController)(Function(x) x.GetTodoItems())

where ToDoController is the ApiController that exposes the data, defined in the same project.

My question is, how can I bind upshot to a Web API that is not in the same project? Is there any way I can use the helpers, or will I have to do everything manually?

I have access to the Model classes (i.e. TodoItem), as they are in a separate assembly, which can be referenced from both projects (SPA & Web API), but I don't have access to the controllers defined in the Web API project.

Szilard Muzsi
  • 1,881
  • 2
  • 16
  • 20

1 Answers1

5

You can use the following:

upshot.dataSources = upshot.dataSources || {};

upshot.metadata({"ModelType:#Namespace":
    {
     "key":["Id"],
     "fields":    
        {"Id":{"type":"Int32:#System"},
         "IpAddress":{"type":"String:#System"},
         "Name":{"type":"String:#System"}
        }
    } (and so on, just map everything)
});

var dataSource = new upshot.RemoteDataSource({
    providerParameters: 
        { url: "Your URL", 
          operationName: "Method Name, example: GetCustomers" },
    entityType: "ModelType:#Namespace",
    bufferChanges: true (or false, whatever you like),
    dataContext: undefined,
    mapping: {}
});

If I were you, If I could I would temporarily create a solution with the Controller project in it, then I would check the JS generated by the UpshotContext helper and copy that into your pages (which is what I did to have the result above, anyway).

Tallmaris
  • 7,605
  • 3
  • 28
  • 58
  • I was really hoping that there'd be a better solution for this :( I wonder if they'll make such an option possible in the future (for consuming services not implemented by you and whose controllers you cannot access) It would really be useful. – Szilard Muzsi Mar 13 '12 at 15:39
  • I can bet they will make the "Web API" discoverable the same way it happens with classic Web Services (publishing the wsdl file). But again, if the wsdl is not published or not accessible you will end up writing your own wiring code. If you think about is a good thing to know how it works in the background ;) – Tallmaris Mar 13 '12 at 17:11
  • 1
    I agree that it's good to know what goes on under the hood, but it is also a pain to maintain and update everything in a consistent manner when something changes on the server side. After all, the `UpshotContext` helper tries to provide just that, it just needs a few more refinements imho :) – Szilard Muzsi Mar 14 '12 at 13:28
  • An example I saw online (sorry can't remember the link) was working ok without the metadata information. This can potentially avoid the maintenance overhead... I'll give it a try and let you know. – Tallmaris Mar 17 '12 at 01:41
  • Any word on running this without the MetaData? Seems like that would be easy not to mention follow more of the KnockOut Mapping style hooks. – Brent Pabst Mar 22 '12 at 23:54
  • Sorry, did not have time to try it out. Anyway the link was actually from the walkthrough in the asp.net website: http://www.asp.net/single-page-application/spa-samples/samples-a-tour-through-spa/scenario-one-load-and-display-data – Tallmaris Mar 23 '12 at 02:12
  • I tried to remove the "metadata" definition in my test project but I got a "No metadata available" exception. So apparently the metadata is needed... strange, since the example I linked above does not use it. – Tallmaris Mar 29 '12 at 08:41
  • @Tallmaris - Could you provide a simple web app where you initialize the upshot from html file. Also, the controller should use ApiCOntroller. I tried to use the code base that you provided above- But it is not working. The http call is getting fired but the result is not getting passed to the mapping object. – Anirban May 25 '12 at 14:14
  • @Tallmaris - It will be great if you could show how to create the client side model and map it accordingly. I am stuck while creating the mapping object – Anirban May 25 '12 at 16:52